I am trying to programmatically (C# CSOM)create folders inside a custom List inside one of my SharePoint 2013 Online site.I have tried 2 code solutions , but both of them are behaving in a weird manner:

  1. This code works perfectly for creating folders inside document library but not for Lists

            var list = ctx.Web.Lists.GetByTitle(listName);
            var folder = list.RootFolder;
            folder = folder.Folders.Add(folderName);
  2. The below code also not working:

             ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                itemCreateInfo.UnderlyingObjectType = FileSystemObjectType.Folder;
                itemCreateInfo.LeafName = folderName;
                ListItem oListItem;
                var list = ctx.Web.Lists.GetByTitle(listName);
                oListItem = list.AddItem(itemCreateInfo);

Both the methods works without error in the console,. but when i browse to the list i am not able to see the Folders. When i try manually to create folder through UI with the same name , i get the message a folder with name already exists.

Edit: I am able to add folder through UI and it is visible. The user credentials passed are of site collection administrator. I have tried in different browsers. Also, 1 more thing in site contents the Last modified time also is not getting changed once i create folder through code.

UPDATE: Got it Fixed. The problem was with the folderName parameter passed to the method. I was reading the folderName from Excel sheet and it had a space appended to its end, that was breaking the code in mine. The Code 1 works only for Libraries not for Lists, Code 2 works fine, lesson learned at least for me. Make Sure to trim the folderName before passing to leafName(i.e itemCreateInfo.LeafName = folderName;).

My final working Code:

private static void CreateFolder(string listName, string folderName, ClientContext ctx)
                List list = ctx.Web.Lists.GetByTitle(listName);
                ListItemCreationInformation info = new ListItemCreationInformation();
                info.UnderlyingObjectType = FileSystemObjectType.Folder;
                info.LeafName = folderName.Trim();//Trim for spaces.Just extra check
                ListItem newItem = list.AddItem(info);
                newItem["Title"] = folderName;
                Console.WriteLine("{0} folder Created", folderName);
            catch (Exception Ex)
  • have you the check in feature enable? With which user are you creating the folder and which user browser the UI? – Salvatore Di Fazio Apr 1 '14 at 11:35
  • Check that the view is not hiding the folders. Create a folder through UI and see if it shows up. – Nadeem Yousuf-AIS Apr 1 '14 at 11:37
  • I have added more details in question – Unnie Apr 1 '14 at 11:43
  • Please check your View. If it Shows items without folders? – Varun Verma Apr 2 '14 at 6:23

Try adding the following code for folder creation.

 //Enable Folder creation for the list
 list.EnableFolderCreation = true;

 //To create the folder
 ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();

 itemCreateInfo.UnderlyingObjectType = FileSystemObjectType.Folder;
 itemCreateInfo.LeafName = folderName;

 ListItem newItem = list.AddItem(itemCreateInfo);
 newItem["Title"] = folderName;
  • This will only work if the current user has Contribute role, otherwise it thows an error. Edit is needed. – Emaborsa May 15 '20 at 8:20

This is a old post, however after several attempts with other server results, I came across this post which led me to my final answer, so I wanted to update.

NOTE: This is part of a process where I'm uploaded from a Local Drive to SharePoint, So the myListLibrary has already been loaded, and the myLocalFile.RelativePath is simply a string that looks something like "\Temp\Folder1\Folder2\myFile.txt"

This is using CSOM ..

//The Target Folder may not exists so we have to account for 
//that before uploading the file. So we start with the RootFolder
Microsoft.SharePoint.Client.Folder myRemoteFolder = myListLibrary.RootFolder;

//Now for each Folder in the File's Path, we switch to that Folder, 
//and create if needed.
foreach (string myFolder in myLocalFile.RelativePath.Replace(myLocalFile.Name, "").Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries))

  myRemoteFolder = mySPClientContext.Web.GetFolderByServerRelativeUrl(myRemoteFolder.ServerRelativeUrl + "/" + myFolder);

Not only did this create my folders perfectly.. but didn't seem to care if they were already there or not.


Alternatively you can use relative path API as well. This works well to create sub folder recursively.

Refer the code:

 Folder _folder = _ctx.Web.GetFolderByServerRelativeUrl("myDocLib\\Folder1");
 _folder.AddSubFolder("Folder L2");

FYI: _ctx is clientContext object

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.