Tell me more ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to create nested folders in my document library so it looks like this:

http://site/PublicDocuments/Folder1/Folder2/Folder3/

The document library "Public Documents" exists but none of the folders exist yet.

Here's the working code for those interested:

    ClientContext clientContext = new ClientContext(url);
    Web web = clientContext.Web;
    var query = clientContext.LoadQuery(web.Lists.Where(p => p.Title == "Public Documents"));
    clientContext.ExecuteQuery();
    List list = query.FirstOrDefault();
    var folder = list.RootFolder;

    clientContext.Load(folder);
    clientContext.ExecuteQuery();

    string[] namesArray = new string[] { "/Folder1", "Folder2", "Folder3" };
    foreach(string name in namesArray)
    {
        folder = folder.Folders.Add(name);            
    }

    clientContext.ExecuteQuery();

Thanks!

share|improve this question

3 Answers

up vote 2 down vote accepted

Try not calling clientContext.Load, for example this works for me:

var folder = list.RootFolder;
clientContext.Load(folder);
clientContext.ExecuteQuery();
folder = folder.Folders.Add("Folder 1");
folder.Folders.Add("Folder 2");
clientContext.ExecuteQuery();
share|improve this answer
Thanks, that works. I'm going to post the code using the foreach loop shortly – ATAI Nov 15 '12 at 22:58

How about something like this:

 public static Folder EnsureFolder(ClientContext ctx, Folder ParentFolder, string FolderPath)
    {
        //Split up the incoming path so we have the first element as the a new sub-folder name 
        //and add it to ParentFolder folders collection
        string[] PathElements = FolderPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);           
        string Head = PathElements[0];
        Folder NewFolder = ParentFolder.Folders.Add(Head);
        ctx.Load(NewFolder);
        ctx.ExecuteQuery();

        //If we have subfolders to create then the length of PathElements will be greater than 1
        if (PathElements.Length > 1)
        {
            //If we have more nested folders to create then reassemble the folder path using what we have left i.e. the tail
            string Tail = string.Empty;
            for (int i = 1; i < PathElements.Length; i++)
                Tail = Tail + "/" + PathElements[i];

            //Then make a recursive call to create the next subfolder
            return EnsureFolder(ctx, NewFolder, Tail);
        }
        else
            //This ensures that the folder at the end of the chain gets returned
            return NewFolder;            
    }

and then you can call it with following:

ctx.Load(TargetList.RootFolder);
ctx.ExecuteQuery();
Folder NewFolder = EnsureFolder(ctx, TargetList.RootFolder, "/Folder1/Folder2/Folder3");
share|improve this answer

Try looping through and adding each folder and then call ExecuteQuery

      string[] namesArray = new string[] {  "Folder1","Folder2"};
      foreach (string name in namesArray)
       {
        //Add Folder
        var folders = list.RootFolder.Folders;
        clientContext.Load(folders);
        clientContext.ExecuteQuery();
        var newFolder = folders.Add(name);
      }
      //Execute request
      clientContext.ExecuteQuery();
share|improve this answer
That code is from the example I was trying to follow. That will only create folders at the root level, resulting in: site/PublicDocuments/Folder1 site/PublicDocuments/Folder2 which is not what I'm looking for – ATAI Nov 15 '12 at 22:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.