Sharepoint2010应用开发四:使用客户对象模型(Client Object Model)向SharePoint列表写数据

1、 打开VS2010File->New Project->WPF Application,确保选择.NET Framework3.5,为新项目命名,如:SPWriteListWPF

2、 WPF设计面板添加4Label4textbox和三个按钮,如下图:

Sharepoint2010应用开发四:使用客户对象模型(Client Object Model)向SharePoint列表写数据

 

3、 添加引用Microsoft.SharePoint.Client. dllMicrosoft.SharePoint.Client.Runtime.dll,在MainWindow.xaml.cs中添加如下using 语句:

 using ClientOM = Microsoft.SharePoint.Client;

using Microsoft.SharePoint.Client;

 

添加变量:

string strSPURL = "";

       string strBlogTitle = "";

       string strBlogContent = "";

       string strBlogAuthor = "";

               

                在添加按钮的事件方法中添加如下代码:

 

代码
private   void  btnAdd_Click( object  sender, RoutedEventArgs e)
        {
            strSPURL 
=  txtbxURL.Text;
            strBlogTitle 
=  txtbxBlogTitle.Text;
            strBlogContent 
=  txtbxBlogContent.Text;
            strBlogAuthor 
=  txtbxBlogAuthor.Text;
            ClientOM.ClientContext mySPContext 
=   new  ClientContext(strSPURL);
            ClientOM.List productsList 
=  mySPContext.Web.Lists.GetByTitle( " Blog " );
            mySPContext.Load(mySPContext.Web);
            mySPContext.Load(productsList);
            mySPContext.ExecuteQuery();
            ListItemCreationInformation newBlogRecord 
=   new  ListItemCreationInformation();
            ClientOM.ListItem newBlogItem 
=  productsList.AddItem(newBlogRecord);
            newBlogItem[
" Title " =  strBlogTitle;
            newBlogItem[
" Content " =  strBlogContent;
            newBlogItem[
" Writer " =  strBlogAuthor;
            newBlogItem.Update();
            mySPContext.ExecuteQuery();
        }

 

 

在上面的代码中,我们没有使用CAML来把数据写到Sharepoint的列表中,这是因为在Sharepoint2010中,Sharepint会自动来创建CAML.

 

4、 运行项目,填写一些数据,提交数据,你可以到你的Sharepoint列表中去查看刚刚添加的数据。

Sharepoint2010应用开发四:使用客户对象模型(Client Object Model)向SharePoint列表写数据

项目文件:http://files.cnblogs.com/Jayan/SPWriteListWPF.zip

你可能感兴趣的:(SharePoint)