c# – 使用客户端对象模型在SharePoint文档库上创建文件夹
发布时间:2020-12-16 00:25:30 所属栏目:百科 来源:网络整理
导读:我想使用客户端对象模型(C#)在SharePoint文档库上创建文件夹.下面是执行此操作的代码. ContentTypeCollection listContentTypes = list.ContentTypes; clientContext.Load(listContentTypes,types = types.Include (type = type.Id,type = type.Name,type =
我想使用客户端对象模型(C#)在SharePoint文档库上创建文件夹.下面是执行此操作的代码.
ContentTypeCollection listContentTypes = list.ContentTypes; clientContext.Load(listContentTypes,types => types.Include (type => type.Id,type => type.Name,type => type.Parent)); var result = clientContext.LoadQuery(listContentTypes.Where(c => c.Name == "Folder")); clientContext.ExecuteQuery(); ContentType folderContentType = result.FirstOrDefault(); ListItemCreationInformation newItemInfo = new ListItemCreationInformation(); newItemInfo.UnderlyingObjectType = FileSystemObjectType.Folder; newItemInfo.LeafName = Foldername; ListItem newListItem = list.AddItem(newItemInfo); newListItem["ContentTypeId"] = folderContentType.Id.ToString(); newListItem["Title"] = Foldername; ewListItem.Update(); clientContext.Load(list); clientContext.ExecuteQuery(); 现在我在库上创建了数千个文件夹. 问题是创建文件夹需要花费太多时间,因为对于每个文件夹,它都会调用SharePoint对象. 解决方法
这很简单.只需将要创建的所有文件夹添加到列表中,然后在运行ExecuteQuery函数之前遍历列表并创建所有列表项:
List<string> folderNames = new List<string>(); folderNames.Add("Folder 1"); folderNames.Add("Folder 2"); folderNames.Add("Folder 3"); ClientContext context = new ClientContext("https://sharepoint.Site/Test"); List list = context.Web.Lists.GetByTitle("Documents"); var folder = list.RootFolder; context.Load(folder); context.ExecuteQuery(); foreach (string folderName in folderNames) { ListItemCreationInformation newItemInfo = new ListItemCreationInformation(); newItemInfo.UnderlyingObjectType = FileSystemObjectType.Folder; newItemInfo.LeafName = folderName; ListItem newListItem = list.AddItem(newItemInfo); newListItem["Title"] = folderName; newListItem.Update(); } context.ExecuteQuery(); 奇迹般有效.您只需要在运行任何CSOM代码之前暂存所有文件夹路径/名称. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |