需要引用:Microsoft.SharePoint.Client
ascx:
<h4>CSOM所有表名</h4> <table> <tr> <td></td> <td> <asp:Button ID="btn_AllTabel_Select" runat="server" Text="检索" OnClick="btn_AllTabel_Select_Click" /> </td> </tr> <tr> <td></td> <td> <asp:Label ID="lbl_AllTabel_List" runat="server" Text=""></asp:Label></td> </tr> </table> <h4>CSOM创建表名</h4> <table> <tr> <td>表名:</td> <td> <asp:TextBox ID="txt_Tabel" runat="server"></asp:TextBox></td> <td> <asp:Label ID="lbl_Notes_cjbm" runat="server" Text=""></asp:Label> </td> </tr> <tr> <td></td> <td> <asp:Button ID="btn_CreateTabel" runat="server" Text="创建" OnClick="btn_CreateTabel_Click" /></td> </tr> </table> <h4>CSOM添加数据</h4> <table> <tr> <td>Name:</td> <td> <asp:TextBox ID="txt_Name" runat="server"></asp:TextBox></td> </tr> <tr> <td>Desc:</td> <td> <asp:TextBox ID="txt_Desc" runat="server"></asp:TextBox></td> <td> <asp:Label ID="lbl_Notes_tjsj" runat="server" Text=""></asp:Label></td> </tr> <tr> <td></td> <td> <asp:Button ID="btn_Add" runat="server" Text="添加" OnClick="btn_Add_Click" /></td> </tr> </table> <h4>CSOM查找数据</h4> <table> <tr> <td>Name:</td> <td> <asp:TextBox ID="txt_Name_Select" runat="server"></asp:TextBox></td> </tr> <tr> <td></td> <td> <asp:Button ID="btn_Select" runat="server" Text="查找" OnClick="btn_Select_Click" /></td> </tr> </table> <h4>CSOM修改数据</h4> <table> <tr> <td>Id:</td> <td> <asp:TextBox ID="txt_Update_Id" runat="server"></asp:TextBox></td> </tr> <tr> <td>Name:</td> <td> <asp:TextBox ID="txt_Update_Name" runat="server"></asp:TextBox></td> </tr> <tr> <td>Desc:</td> <td> <asp:TextBox ID="txt_Update_Desc" runat="server"></asp:TextBox></td> <td> <asp:Label ID="lbl_Notes_xfsj" runat="server" Text=""></asp:Label></td> </tr> <tr> <td></td> <td> <asp:Button ID="btn_Update" runat="server" Text="修改" OnClick="btn_Update_Click" /></td> </tr> </table> <h4>CSOM删除数据</h4> <table> <tr> <td>Id:</td> <td> <asp:TextBox ID="txt_Delete_Id" runat="server"></asp:TextBox></td> </tr> <tr> <td></td> <td> <asp:Button ID="btn_Delete" runat="server" Text="删除" OnClick="btn_Delete_Click" /></td> </tr> </table>
cs:
/// <summary> /// 创建表名 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btn_CreateTabel_Click(object sender, EventArgs e) { //share point 环境 IP string ServerClientContext = "http://server-sp:10001/sites/gzzwz01"; //share point 环境 账户 string UserName = "administrator@sharepoint.com.cn"; //share point 环境 密码 string PassWord = "@ronger0506"; ClientContext context = new ClientContext(ServerClientContext); context.Credentials = new NetworkCredential(UserName, PassWord); var web = context.Web; //表名 string tableName = "tableName"; if (txt_Tabel.Text.Trim() != "") { tableName = txt_Tabel.Text.Trim(); } //表名 ListCollection listct = context.Web.Lists; context.Load(listct); //加载客户端对象list.RootFolder.Folders context.ExecuteQuery(); foreach (List list in listct) { if (list.Title.Equals(tableName, StringComparison.OrdinalIgnoreCase)) { lbl_Notes_cjbm.Text = tableName + "表已存在。"; return; } } ListCreationInformation listCreationInfo = new ListCreationInformation(); listCreationInfo.Title = tableName; listCreationInfo.TemplateType = (int)ListTemplateType.GenericList; List list_Info = web.Lists.Add(listCreationInfo); list_Info.Description = "New Description"; Field field1 = list_Info.Fields.AddFieldAsXml( @"<Field Type='Text' DisplayName='Name'/>", true, AddFieldOptions.DefaultValue); Field field2 = list_Info.Fields.AddFieldAsXml( @"<Field Type='Note' DisplayName='Desc'/>", true, AddFieldOptions.DefaultValue); list_Info.Update(); context.ExecuteQuery(); } /// <summary> /// 添加数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btn_Add_Click(object sender, EventArgs e) { //share point 环境 IP string ServerClientContext = "http://server-sp:10001/sites/gzzwz01"; //share point 环境 账户 string UserName = "administrator@sharepoint.com.cn"; //share point 环境 密码 string PassWord = "@ronger0506"; ClientContext context = new ClientContext(ServerClientContext); context.Credentials = new NetworkCredential(UserName, PassWord); var web = context.Web; string _Tabel = txt_Tabel.Text.Trim(); string _Name = txt_Name.Text.Trim(); string _Desc = txt_Desc.Text.Trim(); var list = web.Lists.GetByTitle(_Tabel); ListItemCreationInformation listItemCI = new ListItemCreationInformation(); ListItem item = list.AddItem(listItemCI); item["Title"] = _Name; item["Name"] = _Name; item["Desc"] = _Desc; item.Update(); context.ExecuteQuery(); } /// <summary> /// 查找数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btn_Select_Click(object sender, EventArgs e) { //share point 环境 IP string ServerClientContext = "http://server-sp:10001/sites/gzzwz01"; //share point 环境 账户 string UserName = "administrator@sharepoint.com.cn"; //share point 环境 密码 string PassWord = "@ronger0506"; ClientContext context = new ClientContext(ServerClientContext); context.Credentials = new NetworkCredential(UserName, PassWord); var web = context.Web; string _Tabel = txt_Tabel.Text.Trim(); string _Name = txt_Name_Select.Text.Trim(); var list = web.Lists.GetByTitle(_Tabel); CamlQuery query = new CamlQuery(); query.ViewXml = string.Format( @"<View> <Query> <Where> <Eq> <FieldRef Name='Name' /> <Value Type='Text'>{0}</Value> </Eq> </Where> <OrderBy> <FieldRef Name='Name' Ascending='FALSE' /> </OrderBy> </Query> </View>", _Name); ListItemCollection items = list.GetItems(query); context.Load(items); context.ExecuteQuery(); for (int i = 0; i < items.Count; i++) { txt_Update_Id.Text = items[i].Id.ToString(); txt_Update_Name.Text = items[i]["Name"].ToString(); txt_Update_Desc.Text = items[i]["Desc"].ToString(); } } /// <summary> /// 修改数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btn_Update_Click(object sender, EventArgs e) { //share point 环境 IP string ServerClientContext = "http://server-sp:10001/sites/gzzwz01"; //share point 环境 账户 string UserName = "administrator@sharepoint.com.cn"; //share point 环境 密码 string PassWord = "@ronger0506"; ClientContext context = new ClientContext(ServerClientContext); context.Credentials = new NetworkCredential(UserName, PassWord); var web = context.Web; string _Tabel = txt_Tabel.Text.Trim(); int _id =Convert.ToInt32(txt_Update_Id.Text); List table_List = context.Web.Lists.GetByTitle(_Tabel); ListItem listItem = table_List.GetItemById(_id); listItem["Name"] = txt_Update_Name.Text.Trim(); listItem["Desc"] = txt_Update_Desc.Text.Trim(); listItem.Update(); context.ExecuteQuery(); } /// <summary> /// 删除 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btn_Delete_Click(object sender, EventArgs e) { //share point 环境 IP string ServerClientContext = "http://server-sp:10001/sites/gzzwz01"; //share point 环境 账户 string UserName = "administrator@sharepoint.com.cn"; //share point 环境 密码 string PassWord = "@ronger0506"; ClientContext context = new ClientContext(ServerClientContext); context.Credentials = new NetworkCredential(UserName, PassWord); var web = context.Web; string _Tabel = txt_Tabel.Text.Trim(); int _id = Convert.ToInt32(txt_Delete_Id.Text); List table_List = context.Web.Lists.GetByTitle(_Tabel); ListItem listItem = table_List.GetItemById(_id); // 删除 listItem.DeleteObject(); context.ExecuteQuery(); } /// <summary> /// 查询 所有列表 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btn_AllTabel_Select_Click(object sender, EventArgs e) { //share point 环境 IP string ServerClientContext = "http://server-sp:10001/sites/gzzwz01"; //share point 环境 账户 string UserName = "administrator@sharepoint.com.cn"; //share point 环境 密码 string PassWord = "@ronger0506"; ClientContext context = new ClientContext(ServerClientContext); context.Credentials = new NetworkCredential(UserName, PassWord); var web = context.Web; context.Load(web.Lists, lists => lists.Include(list => list.Title, list => list.Id)); context.ExecuteQuery(); foreach (List list in web.Lists) { lbl_AllTabel_List.Text = lbl_AllTabel_List.Text + list.Title + ","; } }