1、 打开VS2010,File->New Project->WPF Application,确保选择.NET Framework3.5,为新项目命名,如:SPWriteListWPF
2、 为WPF设计面板添加4个Label,4个textbox和三个按钮,如下图:
3、 添加引用Microsoft.SharePoint.Client. dll和Microsoft.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();
}
{
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列表中去查看刚刚添加的数据。