[英]In C#, how can I create a folder if it does not exist on a SharePoint site
我正在尝试在 C# 中创建一个微服务,它将接受一个包含订单号的 csv 文件,消化 csv,连接到 sharepoint,在 sharepoint 上创建一个新文件夹,然后从任何地方复制名称与订单号相对应的合同(它们可能不会都在 smae 位置)到新文件夹。
此时,在 Stackoverflow 的帮助下,我可以使用CSOM 身份验证管理器从我们的 Sharepoint 成功获取身份验证令牌。 现在我想弄清楚如何创建文件夹。 谷歌搜索有关创建 Sharepoint 文件夹的信息不断提出列表主题,我对此一无所知,甚至不知道我是否真的想要或需要了解任何信息,或者是否可能有不同的工作方式与该网站,因为那是我真正感兴趣的。
因此,假设我在https://example.sharepoint.com/sites/MySite 上有一个 sharepoint 站点。
如何在“共享文档”中存在的名为“Bar”的文件夹中简单地创建一个名为“Foo”的文件夹?
如果我需要了解一些关于列表的信息才能做到这一点,我可以使用 C# 来找到正确的列表吗? 或者我需要找我的管理员获取更多信息吗?
假设 AuthenticationManager 返回一个有效的上下文并且根文件夹已经存在,以下工作:
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using AuthenticationManager = SharepointOrderContractExtractor.Clients.AuthenticationManager;
namespace SharePointOrderContractExtractor.Clients
{
public class FolderManager
{
private readonly AuthenticationManager _authenticationManager;
public FolderManager(
AuthenticationManager sharepointAuthenticationManager
)
{
_authenticationManager = sharepointAuthenticationManager;
}
internal Folder EnsureAndGetTargetFolder(string folderPath)
{
using ClientContext context = _authenticationManager.GetContext();
List<string> folderNames = folderPath.Split("/").ToList();
List documents = context.Web.Lists.GetByTitle(folderNames[0]);
folderNames.RemoveAt(0);
return EnsureAndGetTargetFolder(context, documents, folderNames);
}
private Folder EnsureAndGetTargetFolder(ClientContext context, List list, List<string> folderPath)
{
Folder returnFolder = list.RootFolder;
return (folderPath != null && folderPath.Count > 0)
? EnsureAndGetTargetSubfolder(context, list, folderPath)
: returnFolder;
}
private Folder EnsureAndGetTargetSubfolder(ClientContext context, List list, List<string> folderPath)
{
Web web = context.Web;
Folder currentFolder = list.RootFolder;
context.Load(web, t => t.Url);
context.Load(currentFolder);
context.ExecuteQuery();
foreach (string folderPointer in folderPath)
{
currentFolder = FindOrCreateFolder(context, list, currentFolder, folderPointer);
}
return currentFolder;
}
private Folder FindOrCreateFolder(ClientContext context, List list, Folder currentFolder, string folderPointer)
{
FolderCollection folders = currentFolder.Folders;
context.Load(folders);
context.ExecuteQuery();
foreach (Folder existingFolder in folders)
{
if (existingFolder.Name.Equals(folderPointer, StringComparison.InvariantCultureIgnoreCase))
{
return existingFolder;
}
}
return CreateFolder(context, list, currentFolder, folderPointer);
}
private Folder CreateFolder(ClientContext context, List list, Folder currentFolder, string folderPointer)
{
ListItemCreationInformation itemCreationInfo = new ListItemCreationInformation
{
UnderlyingObjectType = FileSystemObjectType.Folder,
LeafName = folderPointer,
FolderUrl = currentFolder.ServerRelativeUrl
};
ListItem folderItemCreated = list.AddItem(itemCreationInfo);
folderItemCreated.Update();
context.Load(folderItemCreated, f => f.Folder);
context.ExecuteQuery();
return folderItemCreated.Folder;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.