在C#中,如果SharePoint网站上不存在文件夹,如何创建该文件夹

在C#中,如果SharePoint网站上不存在文件夹,如何创建该文件夹,c#,sharepoint,csom,shared-directory,create-directory,C#,Sharepoint,Csom,Shared Directory,Create Directory,我正在尝试用C#创建一个微服务,它将接受一个包含订单号的csv文件,对csv进行摘要处理,连接到sharepoint,在sharepoint上创建一个新文件夹,然后将合同复制到新文件夹中,合同的名称与订单号对应,合同可能来自任何地方(而且可能不会全部位于smae位置) 此时,在Stackoverflow的帮助下,我可以使用。现在我想知道如何在文件夹中创建一个文件夹。谷歌搜索有关创建Sharepoint文件夹的信息时,会不断出现列表的话题,我对列表一无所知,甚至不知道我是否真的想知道或需要知道任何

我正在尝试用C#创建一个微服务,它将接受一个包含订单号的csv文件,对csv进行摘要处理,连接到sharepoint,在sharepoint上创建一个新文件夹,然后将合同复制到新文件夹中,合同的名称与订单号对应,合同可能来自任何地方(而且可能不会全部位于smae位置)

此时,在Stackoverflow的帮助下,我可以使用。现在我想知道如何在文件夹中创建一个文件夹。谷歌搜索有关创建Sharepoint文件夹的信息时,会不断出现列表的话题,我对列表一无所知,甚至不知道我是否真的想知道或需要知道任何事情,或者是否有一种不同的方式来处理该网站,因为这才是我真正感兴趣的

那么,假设我有一个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;
        }
    }
}
使用Microsoft.SharePoint.Client;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用AuthenticationManager=SharepointOrderContractExtractor.Clients.AuthenticationManager;
命名空间SharePointOrderContractExtractor.Clients
{
公共类FolderManager
{
私有只读AuthenticationManager\u AuthenticationManager;
公共文件夹管理员(
AuthenticationManager共享点AuthenticationManager
)
{
_authenticationManager=sharepointAuthenticationManager;
}
内部文件夹EnsureEndGetTargetFolder(字符串folderPath)
{
使用ClientContext=_authenticationManager.GetContext();
List folderNames=folderPath.Split(“/”).ToList();
列表文档=context.Web.Lists.GetByTitle(folderNames[0]);
folderNames.RemoveAt(0);
返回EnsureEndGetTargetFolder(上下文、文档、文件夹名称);
}
私有文件夹EnsureEndGetTargetFolder(ClientContext上下文、列表列表、列表文件夹路径)
{
Folder returnFolder=list.RootFolder;
return(folderPath!=null&&folderPath.Count>0)
?确保指定目标子文件夹(上下文、列表、文件夹路径)
:returnFolder;
}
私有文件夹EnsureEndGetTarget子文件夹(ClientContext上下文、列表列表、列表文件夹路径)
{
Web=context.Web;
文件夹currentFolder=list.RootFolder;
Load(web,t=>t.Url);
加载(当前文件夹);
context.executeEqualy();
foreach(folderPath中的字符串folderPointer)
{
currentFolder=FindOrCreateFolder(上下文、列表、currentFolder、folderPointer);
}
返回当前文件夹;
}
专用文件夹FindOrCreateFolder(ClientContext上下文、列表、文件夹currentFolder、字符串folderPointer)
{
FolderCollection folders=currentFolder.folders;
加载(文件夹);
context.ExecuteQuery();
foreach(文件夹中存在的文件夹文件夹)
{
if(existingFolder.Name.Equals(folderPointer、StringComparison.InvariantCultureIgnoreCase))
{
返回现有文件夹;
}
}
返回CreateFolder(上下文、列表、currentFolder、folderPointer);
}
私有文件夹CreateFolder(ClientContext上下文、列表、文件夹currentFolder、字符串folderPointer)
{
ListItemCreationInformation itemCreationInfo=新建ListItemCreationInformation
{
underlineObjectType=FileSystemObjectType.Folder,
LeafName=folderPointer,
FolderUrl=currentFolder.ServerRelativeUrl
};
ListItem folderItemCreated=list.AddItem(itemCreationInfo);
folderItemCreated.Update();
Load(folderItemCreated,f=>f.Folder);
context.ExecuteQuery();
返回folderItemCreated.Folder;
}
}
}

用一种不知道其基本和最常用数据结构的语言编程将很困难。。。如果您不了解列表,为什么不能先学习这个主题呢?SharePoint是一个带有API的文档管理应用程序。它既不是数据库也不是文件系统。如果你不了解C#中列表的基本知识,你就无法进行编程。我不知道Sharepoint上的列表。我真的需要了解他们吗?他们毫无共同之处。这就像询问Google表单是否与
字符串[,]
数组相同。谷歌表单可能更接近SharePoint列表——表单将答案存储在(谷歌的)列表中,但这就是重点。我想知道是否有一个Sharepoint API允许我简单地使用共享文件夹或文件,或者我是否需要投入时间学习更多有关Sharepoint的知识,而不是我手头的问题所需要知道的知识?