在 C# 中,如果 SharePoint 站点上不存在文件夹,我该如何创建该文件夹
In C#, how can I create a folder if it does not exist on a SharePoint site
我正在尝试在 C# 中创建一个微服务,它将接受一个包含订单号的 csv 文件,消化 csv,连接到 sharepoint,在 sharepoint 上创建一个新文件夹,然后复制名称与订单号相对应的合同从他们所在的任何地方(他们可能不会都在 smae 的地方)到新文件夹。
此时,在 Whosebug 的帮助下,我可以使用 CSOM Authentication Manager 从我们的 Sharepoint 成功获取身份验证令牌。现在我想弄清楚如何创建文件夹。谷歌搜索有关创建 Sharepoint 文件夹的信息不断提出列表的主题,我对此一无所知,甚至不知道我是否真的想要或需要知道任何事情,或者是否有不同的工作方式我真正感兴趣的是网站。
所以,假设我在 https://example.sharepoint.com/sites/MySite 有一个共享点网站。
我怎样才能简单地在“共享文档”中存在的名为“Bar”的文件夹中创建一个名为“Foo”的文件夹?
如果我需要了解一些关于列表的知识才能执行此操作,我可以使用 C# 找到正确的列表吗?或者我是否需要追问我的管理员以获取更多信息?
假设 AuthenticationManager returns 一个有效的上下文并且根文件夹已经存在,以下工作:
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;
}
}
}
我正在尝试在 C# 中创建一个微服务,它将接受一个包含订单号的 csv 文件,消化 csv,连接到 sharepoint,在 sharepoint 上创建一个新文件夹,然后复制名称与订单号相对应的合同从他们所在的任何地方(他们可能不会都在 smae 的地方)到新文件夹。
此时,在 Whosebug 的帮助下,我可以使用 CSOM Authentication Manager 从我们的 Sharepoint 成功获取身份验证令牌。现在我想弄清楚如何创建文件夹。谷歌搜索有关创建 Sharepoint 文件夹的信息不断提出列表的主题,我对此一无所知,甚至不知道我是否真的想要或需要知道任何事情,或者是否有不同的工作方式我真正感兴趣的是网站。
所以,假设我在 https://example.sharepoint.com/sites/MySite 有一个共享点网站。
我怎样才能简单地在“共享文档”中存在的名为“Bar”的文件夹中创建一个名为“Foo”的文件夹?
如果我需要了解一些关于列表的知识才能执行此操作,我可以使用 C# 找到正确的列表吗?或者我是否需要追问我的管理员以获取更多信息?
假设 AuthenticationManager returns 一个有效的上下文并且根文件夹已经存在,以下工作:
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;
}
}
}