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
Table of Contents
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.
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:
- Folder – It refers to newly created folder of type Folder class.
- 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
Top 10 tools and libraries for SharePoint Framework (SPFx)
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.
Hope you enjoyed reading it.
Please don't forget to Share, Follow and Like to get updated with latest posts.
It gives me motivation to share more knowledge with you.
About Author
Satyendra Mishra
Project Management Professional (PMP) and Microsoft certified, motivated, energetic and accomplished Project Manager / Architect with 15+ years of work experience in Management, Architecture, Analytics, Development and Maintenance. I have been fortunate to be a part of over 25+ .Net / SharePoint projects delivery with various companies across different industry sectors. This has provided me a valuable insight and experience especially in successful implementation of SharePoint based solutions.
My experience in Web application implementation includes technology strategy and road-map definition, business and technical requirements identification, governance, platform architecture, solution design, configuration, development, quality assurance, training, post-production support, team lead and overall project delivery along with project management.
Satyendra Mishra holds a B.Tech. in Computer Science & Engineering and PG Diploma in Advance Computing from Center for Development and Advance Computing, Pune, India. He is also a certified Project Management Professional (PMP).
I love to share Project Management Tips and Tricks by writing Blogs in the Project Management India Community. I have written around 300+ articles in the Project Management, .Net, SharePoint and related client side technologies.