在 Sharepoint 在线列表中创建文件夹结构

Create folder structure in a Sharepoint online list

我正在尝试在 Sharepoint 在线列表下创建文件夹结构。 我根据这篇文章创建了一个名为 Shared1 的列表:

http://blogs.technet.com/b/catastrophic_failure_joannav/archive/2013/10/23/how-to-create-a-custom-list-in-sharepoint-online-quot-w15-quot.aspx

我正在使用此代码尝试创建底层结构:

using (var clientContext = new ClientContext("https://myEnv.sharepoint.com"))
{
    var passWord = new SecureString();
    foreach (char c in "myPSW".ToCharArray()) passWord.AppendChar(c);

    clientContext.Credentials = new SharePointOnlineCredentials("myAcc@myenv.onmicrosoft.com", passWord);
    Web web = clientContext.Web;
    clientContext.Load(web);
    clientContext.ExecuteQuery();
    var folder = CreateFolder(clientContext.Web, "Shared1", "FolderA/SubFolderA/SubSubFolderA");
}

/// <summary>
/// Create Folder client object
/// </summary>
/// <param name="web"></param>
/// <param name="listTitle"></param>
/// <param name="fullFolderUrl"></param>
/// <returns></returns>
public static Folder CreateFolder(Web web, string listTitle, string fullFolderUrl)
{
    if (string.IsNullOrEmpty(fullFolderUrl))
        throw new ArgumentNullException("fullFolderUrl");
    var list = web.Lists.GetByTitle(listTitle);
    return CreateFolderInternal(web, list.RootFolder, fullFolderUrl);
}

private static Folder CreateFolderInternal(Web web, Folder parentFolder, string fullFolderUrl)
{
    var folderUrls = fullFolderUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
    string folderUrl = folderUrls[0];
    var curFolder = parentFolder.Folders.Add(folderUrl);
    web.Context.Load(curFolder);
    web.Context.ExecuteQuery();

    if (folderUrls.Length > 1)
    {
        var subFolderUrl = string.Join("/", folderUrls, 1, folderUrls.Length - 1);
        return CreateFolderInternal(web, curFolder, subFolderUrl);
    }
    return curFolder;
}

public static Folder GetFolder(Web web, string fullFolderUrl)
{
    if (string.IsNullOrEmpty(fullFolderUrl))
        throw new ArgumentNullException("fullFolderUrl");

    if (!web.IsPropertyAvailable("ServerRelativeUrl"))
    {
        web.Context.Load(web, w => w.ServerRelativeUrl);
        web.Context.ExecuteQuery();
    }
    var folder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + fullFolderUrl);
    web.Context.Load(folder);
    web.Context.ExecuteQuery();
    return folder;
}

在执行时我得到这个错误:

Unhandled Exception: Microsoft.SharePoint.Client.ServerException: List 'Shared1'
 does not exist at site with URL 'https://myEnv.sharepoint.com'.
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream res
ponseStream)
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
   at SharepointFolderRename.Program.CreateFolderInternal(Web web, Folder parent
Folder, String fullFolderUrl) in \vmware-host\shared folders\Documents\Visual S
tudio 2012\Projects\Trunk2015\SharepointFolderRename\Program.cs:line 84
   at SharepointFolderRename.Program.Main(String[] args) in \vmware-host\shared
 folders\Documents\Visual Studio 2012\Projects\Trunk2015\SharepointFolderRename\
Program.cs:line 49

这是 SP 中列表的打印件。

我错过了什么?

您需要在 CreateFolder 方法中加载列表。

public static Folder CreateFolder(Web web, string listTitle, string fullFolderUrl)
{
    if (string.IsNullOrEmpty(fullFolderUrl))
        throw new ArgumentNullException("fullFolderUrl");
    var list = web.Lists.GetByTitle(listTitle);
    clientContext.Load(list);
    clientCOntext.Execute();
    return CreateFolderInternal(web, list.RootFolder, fullFolderUrl);
}

您收到这些错误的原因与指定的不正确网站 url 有关。当您的列表存在于子站点 Shared1 上时,您正试图在根站点上访问它,下图说明了这一点

因此,解决方案是为正确的网站初始化客户端上下文,因此替换:

using (var clientContext = new ClientContext("https://myEnv.sharepoint.com/"))
{
    //...
}

using (var clientContext = new ClientContext("https://myEnv.sharepoint.com/Shared1"))
{
    //...
}

It is assumed that you want to ctreate folders for list located on Shared1 sub site

例子

下面的示例演示了如何在 News 子站点下的 Documents 库中创建文件夹层次结构:(https://contoso.sharepoint.com/news)

   Archive
   |
   2009
      |
      09  

用法

using (var ctx = GetContext("https://contoso.sharepoint.com/news", userName, password))
{
      var list = ctx.Web.Lists.GetByTitle("Documents");
      var folder = list.CreateFolder("Archive/2015/09");
      Console.WriteLine(folder.ServerRelativeUrl);
}

哪里

public static ClientContext GetContext(Uri webUri, string userName, string password)
{
   var securePassword = new SecureString();
   foreach (var ch in password) securePassword.AppendChar(ch);
   return new ClientContext(webUri) { Credentials = new SharePointOnlineCredentials(userName, securePassword) };
}

ListExtensions.cs 文件:

using System;
using Microsoft.SharePoint.Client;

namespace SharePoint.ClientExtensions
{
    public static class ListExtensions
    {


        /// <summary>
        /// Create Folder in List
        /// </summary>
        /// <param name="list"></param>
        /// <param name="folderUrl"></param>
        /// <returns></returns>
        public static Folder CreateFolder(this List list, string folderUrl)
        {
            if (string.IsNullOrEmpty(folderUrl))
                throw new ArgumentNullException("folderUrl");
            if (!list.IsPropertyAvailable("RootFolder"))
            {
                list.Context.Load(list.RootFolder);
                list.Context.ExecuteQuery();
            }
            return CreateFolderInternal(list.RootFolder,folderUrl);
        }

        private static Folder CreateFolderInternal(Folder parentFolder, string folderUrl)
        {  
            var folderUrlParts = folderUrl.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            var curFolder = parentFolder.Folders.Add(folderUrlParts[0]);
            parentFolder.Context.Load(curFolder);
            parentFolder.Context.ExecuteQuery();
            if (folderUrlParts.Length > 1)
            {
                var subFolderUrl = string.Join("/", folderUrlParts, 1, folderUrlParts.Length - 1);
                return CreateFolderInternal(curFolder, subFolderUrl);
            }
            return curFolder;
        }
    }
}