使用服务器端对象模型,编写程序管理SharePoint列表

使用服务器端对象模型,编写程序管理SharePoint列表

列表是SharePoint核心的工艺品。服务器端对象模型是与列表交互的方法之一。你可以在服务器上创建不同类型的应用程序,与服务器对象模型交互。比如WinForm或WPF/Web parts,甚至是Event Receiver。
1. 管理员身份打开VS,新建项目WPF应用程序,确保选择.NET Framework 3.5,命名WPFSPTestApp,点击确定。
2. 添加引用Microsoft.SharePoint.dll,点击确定。
3. 添加控件。对应MainWindow.xaml代码为:

4. 双击Exit按钮,添加代码:
private void btnExit_Click(object sender, RoutedEventArgs e)
{
    Application.Current.Shutdown();
}
5. 双击Clear按钮,添加代码:
private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            txtbxListName.Text = "";
            txtbxSPURL.Text = "";
            txtbxProdName.Text = "";
            txtbxProductSku.Text = "";
            txtbxProductPrice.Text = "";
        }
6. 在MainWindow.xaml.cs文件中添加引用
using Microsoft.SharePoint;
7. 添加5个类级别的变量。
public partial class MainWindow : Window
    {
        string strSPSiteURL = "";
        string strSPListName = "";
        string strProductName = "";
        string strProductSKU = "";
        string strProductPrice = "";
    }
8. 双击Load按钮,添加代码:
private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            strSPSiteURL = txtbxSPURL.Text;
            strSPListName = txtbxListName.Text;
            strProductName = txtbxProdName.Text;
            strProductSKU = txtbxProductSku.Text;
            strProductPrice = txtbxProductPrice.Text;
            using (SPSite site = new SPSite(strSPSiteURL))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPList list = web.Lists[strSPListName];
                    SPListItem Item = list.Items.Add();
                    Item["Title"] = strProductName;
                    Item["Product_SKU"] = strProductSKU;
                    Item["Price"] = strProductPrice;
                    Item.Update();
                    web.AllowUnsafeUpdates = false;
                }
            }
            MessageBox.Show("Load Successfully");
        }
全部代码是这样的:
9. F5调试。输入类似产品信息。

10. 打开站点导航到Products列表,可以看到更新了。

故障分析:

如果遇到下列问题

请修改项目属性

原文地址:https://www.cnblogs.com/crazygolf/p/3856716.html