使用客户端对象模型回写SharePoint列表
客户端对象模型是一个有效的方式回写SharePoint列表。
1. 管理员身份打开VS,新建WPF应用程序SPWriteListApp,确保选择.NET Framework 3.5。
2. 添加以下类型控件。
界面如下:
对应代码:
using ClientOM = Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client;
4. 完整代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;
using ClientOM = Microsoft.SharePoint.Client;
namespace SPWriteListApp
{
public partial class MainWindow : Window
{
string strSPURL = "";
string strProdName = "";
string strProdSKU = "";
string strProdPrice = "";
public MainWindow()
{
InitializeComponent();
}
private void btnExit_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void btnClear_Click(object sender, RoutedEventArgs e)
{
txtbxURL.Text = "";
txtbxProdName.Text = "";
txtbxProdSKU.Text = "";
txtbxProdPrice.Text = "";
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
strSPURL = txtbxURL.Text;
strProdName = txtbxProdName.Text;
strProdSKU = txtbxProdSKU.Text;
strProdPrice = txtbxProdPrice.Text;
ClientOM.ClientContext mySPContext = new ClientContext(strSPURL);
ClientOM.List productsList = mySPContext.Web.Lists.GetByTitle
("Products");
mySPContext.Load(mySPContext.Web);
mySPContext.Load(productsList);
mySPContext.ExecuteQuery();
ListItemCreationInformation newProdRecord =
new ListItemCreationInformation();
ClientOM.ListItem newProdItem = productsList.AddItem(newProdRecord);
newProdItem["Title"] = strProdName;
newProdItem["Product_SKU"] = strProdSKU;
newProdItem["Price"] = strProdPrice;
newProdItem.Update();
mySPContext.ExecuteQuery();
MessageBox.Show("Done");
}
}
}
查看列表