Sharepoint练习——客户端对象模型
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sharepoint练习——客户端对象模型相关的知识,希望对你有一定的参考价值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.SharePoint.Client; using System.IO;
#region 上传文档 public static void FileAdd() { List Item1 = Web.GetList("sites/tangkang/DocLib"); FileCreationInformation FileCf = new FileCreationInformation(); string ListName = @"C:\Users\dcadmin\Desktop\123 (3).txt"; FileStream Stream = new FileStream(ListName, FileMode.Open, FileAccess.Read); BinaryReader Reader = new BinaryReader(Stream); Byte[] Byteitem = Reader.ReadBytes((int)Stream.Length); Reader.Close(); Stream.Close(); FileCf.Content = Byteitem; FileCf.Overwrite = true; FileCf.Url = "test02.text"; Item1.RootFolder.Files.Add(FileCf); Context.ExecuteQuery(); Console.WriteLine("上传成功"); Console.ReadKey(); } #endregion
#region 删除列表项信息 /// <summary> /// 删除列表项信息 /// </summary> public static void ItemNewDelete() { ListItem list_item = Item.GetItemById(2); list_item.DeleteObject(); Context.ExecuteQuery(); Console.WriteLine("删除成功"); } #endregion
#region 修改列表项信息 /// <summary> /// 修改列表项信息 /// </summary> public static void ItemNewUpdate() { ListItem list_item = Item.GetItemById(1); list_item["FirstName"] = "少"; // Context.Load(list_item);如果需要使用list_item这个对象,一定要先Load //Context.ExecuteQuery();//Load完后必须执行这一步 list_item.Update(); Context.ExecuteQuery(); Console.WriteLine("修改成功"); } #endregion
#region 添加列表项信息 /// <summary> /// 添加列表项信息 /// </summary> public static void ItemNewAdd() { ListItemCreationInformation itemCreation = new ListItemCreationInformation(); ListItem newitem = Item.AddItem(itemCreation); newitem["Title"] = "唐"; newitem["FirstName"] = "康"; newitem.Update(); Context.ExecuteQuery(); Console.WriteLine("添加成功"); } #endregion
#region 查询列表项信息 /// <summary> /// 查询列表栏信息 /// </summary> public static void ItemNewSelect() { CamlQuery Query = new CamlQuery(); Query.ViewXml = ""; ListItemCollection ListItem = Item.GetItems(Query); Context.Load(ListItem); Context.ExecuteQuery(); foreach (ListItem item in ListItem) { Console.WriteLine(item["Title"]); } } #endregion
#region 添加列表栏信息 /// <summary> /// 添加列表栏信息 /// </summary> public static void ItemAdd() { Field field = Item.Fields.AddFieldAsXml("<Field DisplayName=‘籍贯‘ Type=‘Number‘ />", true, AddFieldOptions.DefaultValue); FieldNumber fieldNumber = Context.CastTo<FieldNumber>(field); fieldNumber.Update(); Context.ExecuteQuery(); Console.WriteLine("添加成功"); } #endregion
#region 删除列表 /// <summary> /// 删除列表 /// </summary> public static void ListDelete() { List list_Delete = Context.Web.Lists.GetByTitle("Test01");//根据名称来获取 list_Delete.DeleteObject(); Context.ExecuteQuery(); Console.WriteLine("删除成功"); } #endregion
#region 修改列表信息 /// <summary> /// 修改列表信息 /// </summary> public static void ListUpdate() { List list_Update = Context.Web.Lists.GetByTitle("Summer01");//根据名称来获取 list_Update.Title = "Test01"; list_Update.Update(); Context.ExecuteQuery(); Console.WriteLine("修改成功"); } #endregion
#region 添加列表 /// <summary> /// 添加列表 /// </summary> public static void ListAdd() { ListCreationInformation Creation = new ListCreationInformation(); Creation.Title = "Summer01"; Creation.TemplateType = (int)ListTemplateType.DocumentLibrary; List newlist = Web.Lists.Add(Creation); Context.ExecuteQuery();//与sharepoint通讯 Console.WriteLine("新建成功"); } #endregion
#region 查询列表信息 /// <summary> /// 查询列表信息 /// </summary> public static void ListSelect() { Context.Load(Web.Lists, lis => lis.Include(li => li.Title, li => li.ItemCount, list => list.Created)); Context.ExecuteQuery(); foreach (List lis in Web.Lists) { Console.WriteLine(lis.Title + lis.ItemCount); } } #endregion
#region 查询网站信息 /// <summary> /// 查询网站信息 /// </summary> public static void WebSelect() { Context.Load(Web); Context.ExecuteQuery();//与sharepoint通讯 Console.WriteLine(Web.Title); } #endregion
#region 修改网站信息 /// <summary> /// 修改网站信息 /// </summary> public static void WebUpdate() { Context.Load(Web, w => w.Title, wde => wde.Description); Web.Title = "Summer"; Web.Description = "Summer"; Web.Update(); Context.ExecuteQuery();//与sharepoint通讯 Console.WriteLine("修改成功"); } #endregion
#region 添加网站 /// <summary> /// 添加网站 /// </summary> public static void WebAdd() { WebCreationInformation Creation = new WebCreationInformation(); Creation.Url = "Summer"; Creation.Title = "Summer"; Creation.Description = "Summer"; Web newweb = Web.Webs.Add(Creation); Context.Load(newweb, w => w.Title, wde => wde.Description); Context.ExecuteQuery();//与sharepoint通讯 Console.WriteLine("新建成功"); } #endregion
以上是关于Sharepoint练习——客户端对象模型的主要内容,如果未能解决你的问题,请参考以下文章
powershell PS Sharepoint获取Sharepoint对象模型的客户端设置
Sharepoint 2010 客户端对象模型 - 将列表数据分配给网格视图
SharePoint 2010 - 客户端对象模型 - 将附件添加到 ListItem