2

How to create folders and subfolders in SharePoint document library using client object model?

For example:

India
   Andhra pradesh
      Hyderabad
        Hitech City
6

How to create Folder (including nested) via CSOM in SharePoint 2010/2013

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

Usage

Assume the following folder structure has to be created in Documents library:

 - Archive
 |
 --- 2008
   |
   --- 09

The example demonstrates how to create the specified folder structure:

 using (var ctx = new ClientContext(siteUrl))
 {
     var folder = CreateFolder(ctx.Web, "Documents", "Archive/2008/09");
 }
3
  • Is it possible to achieve the same ( create the nested structure of folders and subfolders) using ecma script / jsom Feb 21 '18 at 13:01
  • 1
    @SaMolPP, surely! Basically this script could be converted or if you have a more specific requirements It might be a good idea to create a separate question Feb 21 '18 at 13:03
  • Vadim, i have posted a new question/thread on this using ECMA script , please throw some light on that question sharepoint.stackexchange.com/questions/237907/… Mar 15 '18 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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