Create folder in SharePoint document library using CSOM

This SharePoint tutorial explains, how to create folders and subfolders inside a document library in SharePoint Online using (csom) .Net managed client object model code.

Here I have a SharePoint document library and by using the client-side code we will create a folder and within that folder, we will also create another subfolder using C#.

Create a folder in SharePoint document library using csom

This demo we will check inside a windows application using visual studio 2017, you can also create a console application in c#.net.

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dlls

Below is the full code.

using (ClientContext clientContext = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
{
SecureString passWord = new SecureString();
foreach (char c in "********".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("bijay@OnlySharePoint2013.onmicrosoft.com", passWord);
var web = clientContext.Web;
var lst = web.Lists.GetByTitle("MyDemoDocuments");
var fld1 = lst.RootFolder.Folders.Add("FirstLevel1");
var fld2 = fld1.Folders.Add("SecondLevel2");
fld1.Update();
fld2.Update();
clientContext.ExecuteQuery();
}

After running the code it will create a folder and subfolder inside the SharePoint document library.

You may like following SharePoint csom tutorials:

This SharePoint tutorial explains, how to create a folder and subfolders in the SharePoint document library using csom.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>