How to add subfolder in SharePoint library using CSOM?

 

 

Being a SharePoint professional, sometimes we need to create folders in SharePoint library. You may be thinking that it is not such a complex thing that you need to refer any article for. But as a developer you are not always expected to create folder in SharePoint library using browser. You need automatic code logic to create a folder. It may use SharePoint Server Side Object Model or Client Side Object Model (CSOM). In this article, you will see how to add subfolder in SharePoint library using CSOM.

 




 

I am providing below a C# function which adds folder in SharePoint library using SharePoint CSOM Client Side Object Model. Let’s look at below:

How to upgrade SharePoint Framework (SPFx) Solution

Input Parameters

context – You need to pass SharePoint client context of type ClientContext class. It will be used to perform operations on SharePoint.

listId – You also need to pass SharePoint library Id in string format e.g. “{9E2A3DBB-5645-42BC-B24A-D62AAB23AB03}”. It will be used to get instance of SharePoint library in the code.

Why SharePoint Online Capacity Planning and Load Testing

subFolderName – Provide the name of folder, you want to get created in string format e.g. “My Documents Folder”.

 




 

Output

This function returns either of two below values:

  1. Folder – It refers to newly created folder of type Folder class.
  2. Null – if SharePoint library id is invalid or SharePoint context is invalid.




Code

private Folder CreateSignedDocumentsFolder(ClientContext context, string listId, string subFolderName)

{

Folder output = null;

if (!string.IsNullOrEmpty(listId))

{

Guid guid = new Guid(listId);

List list = context.Web.Lists.GetById(guid);

 

Folder rootFolder = list.RootFolder;

context.Load(rootFolder.Folders);

context.Load(rootFolder, rf => rf.ServerRelativeUrl);

context.ExecuteQuery();

 

string url = rootFolder.ServerRelativeUrl + “/” + subFolderName;

var folder = context.Web.GetFolderByServerRelativeUrl(url);

context.Load(folder, f => f.Exists);

try

{

context.ExecuteQuery();

if (folder.Exists)

{

context.Load(folder, sf => sf.ServerRelativeUrl);

context.ExecuteQuery();

output = folder;

return output;

}

Folder subFolder = rootFolder.Folders.Add(subFolderName);

rootFolder.Update();

context.Load(subFolder, sf => sf.ServerRelativeUrl);

context.ExecuteQuery();

output = subFolder;

return output;

}

catch (ServerUnauthorizedAccessException uae)

{

throw;

}

catch (Exception ex)

{

Folder subFolder = rootFolder.Folders.Add(subFolderName);

rootFolder.Update();

context.Load(subFolder, sf => sf.ServerRelativeUrl);

context.ExecuteQuery();

output = subFolder;

return output;

}

}

else

{

return output;

}

}

 




 

What is new in SharePoint Server 2019

How to become a SharePoint Developer?

Conclusion

Hope above code has helped you in your programming. In this post, you have seen how to add subfolder in SharePoint library using CSOM.

Please LIKE, SHARE or SUBSCRIBE at our Social Media platforms to keep updated on SharePoint topics.

 

THREE QUERIES offers easy access to information about SharePoint and associated technologies, project management, agile and scrum methodologies that helps developers, administrators, architects, technical managers, business analysts and end users. It has grown from there. We provide an important knowledge base for those involved in managing, architecture and developing software projects of all kinds. With weekly/daily exclusive updates, we keep you in touch with the latest business, management, technology thinking.

WE ARE CONNECTED ~ Follow us on social media to get regular updates and opinion on what's happening in the world of SharePoint, front-end, back end web technologies and project management. If you like this article, please share it and follow us at Facebook, Twitter, Instagram, Pinterest and LinkedIn

About Satyendra Mishra

Microsoft certified, motivated, energetic and accomplished SharePoint Consultant and Architect with 13+ years of work experience in Management, Architecture, Analytics, Development and Maintenance. He has been fortunate to be a part of over 20+ Web/Mobile/Software SharePoint and .Net projects delivery with various companies across different industry sectors. This has provided him a valuable insight and experience especially in successful implementation of technology solutions. Having very strong System Architecture, Technological, Management & Analytical Skills, Knowledge of Office 365, SharePoint, .Net, JavaScript, React, Angular, Azure, SQL. In 2016, he decided to start sharing the knowledge back to society in the form for content writing and blogging.
View all posts by Satyendra Mishra →

Leave a Reply

Your email address will not be published. Required fields are marked *