在SharePoint 2013中以编程方式创建文件夹

时间:2014-02-24 23:17:03

标签: c# sharepoint-2013 csom

目前我的代码在运行时在Documents目录中创建了一个文件夹:

using (var context = new Microsoft.SharePoint.Client.ClientContext(sharePointSite))
{
    context.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(user, password);

    Web web = context.Web;

    Microsoft.SharePoint.Client.List docs = web.Lists.GetByTitle(<upper level folder>);
    docs.EnableFolderCreation = true;

    docs.RootFolder.Folders.Add(folderName);

    context.ExecuteQuery();

    return true;
}

我在使用此代码创建的文件夹中创建子文件夹时遇到了麻烦。因此,如果我想在Feb下创建名为Documents的文件夹,就可以这样做。但是,如果我想在新文件夹Week 2下创建一个名为Feb的文件夹。它不会那样做。我收到这个错误:

{"List 'Feb' does not exist at site with URL 'https://my.sharepoint.com/sites/labels'."}

我意识到问题可能是docs.RootFolder.Folders.Add(folderName);,因为Feb不是根文件夹,当它查找它时会抛出异常。

所以我希望有人可以帮我解决一些代码,将子文件夹添加到已创建的文件夹中。我使用Visual Stuidos 2010并且无法升级到2012,否则我会。我有可以在VS 2010中引用的2013 Microsoft.Sharepoint.Client dll。

3 个答案:

答案 0 :(得分:26)

如何在SharePoint 2010/2013中通过CSOM创建文件夹(包括嵌套)

/// <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;
}

用法

 using (var ctx = new ClientContext("https://contoso.onmicrosoft.com/"))
 {
       ctx.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials("username", "password");
       var folder = CreateFolder(ctx.Web, "Shared Documents", "FolderA/SubFolderA/SubSubFolderA");
 }

如何获取文件夹客户端对象

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;
}

用法

var existingFolder = GetFolder(ctx.Web, "Shared Documents/FolderA/SubFolderA/SubSubFolderA");

答案 1 :(得分:1)

您可以像这样创建文件夹:

Microsoft.SharePoint.Client.List docs = web.Lists.TryGetList("upper level folder");
FolderCollection folderColl = docs.RootFolder.SubFolders;
Folder newFolder = folderColl.Add("upper level folder/Feb/Week 2");

答案 2 :(得分:0)

要在 QuickLinks 列表中的已知特定节点后添加文件夹,请尝试以下操作:

ClientContext context = new ClientContext("https://companySP.com/sites/RootSite");
NetworkCredential _myCredentials = new NetworkCredential(userName, password);

context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = _myCredentials;
context.Load(context.Web, w => w.Title);
context.ExecuteQuery();
Console.WriteLine($"Connected to {context.Web.Title}");
/*  Connection made to Sharepoint Site  */

NavigationNodeCollection ql = context.Web.Navigation.QuickLaunch;
context.Load(ql);
context.ExecuteQuery();
Console.WriteLine("Current nodes:\n");
NavigationNode addAfterNode = null;
foreach (NavigationNode navNode in ql)
{
   Console.WriteLine(navNode.Title + " - " + navNode.Url);
   if (navNode.Title == "Existing List Name In Quicklinks")
   {
      addAfterNode = navNode;
      break;
   }
}
/*  Navigation Node Found to Add After in Quick Links  */

NavigationNodeCreationInformation nnci = new NavigationNodeCreationInformation();
nnci.Title = "New List Title";
nnci.Url = "URL of New List Title";

nnci.PreviousNode = addAfterNode;  // Sets the previous node to add after
ql.Add(nnci);
 
context.Load(ql);
context.ExecuteQuery();

context.Web.Update();