SharePoint How to create a document set programmatically

In SharePoint it is not only possible to create files, documents or listitems programmatically. You also can build up a solution in which you create folders or even document sets.

Why should this interested me? Well, the advantages of document sets should be known and if you decided to use them, then it might also be a kind of interesting how to use these things by c#. If you need a scenario: If your customer uses CRM and wants to store the customer related documents in a SharePoint, it might really helpful to create for each customer a document set in which the contracts, marketing flyers, offers or the mails can be uploaded. In this case if you create a new customer account in your CRM you also want to create a document set in SharePoint automatically. There is no need for the user to go to CRM, create a new account and after that go to SharePoint and create a new document set and type in all the customer informatione like name, id, adress, contact person, etc.. again and again. It just happens.

Ok enough of theory, let’s see how we can create a document set by code. But at first we need to activate the document set feature in SharePoint if not already done. In the site settings you should go to the Site Collection features link and activate the document set feature. After that you can add the document set content type to your library. This post helps you.

Ok, we can start. In my example i simply used a console application just to show how it works.

[sourcecode language=”csharp”]

using (SPSite site = new SPSite(“http://yourserver/sites/sitename/”))
{
SPWeb web = site.RootWeb;
SPList docList = web.Lists[“Documents”];
if(docList != null)
{
CreateDocumentSet(docList, “ILikeSharepoint”);

}

}

public static DocumentSet CreateDocumentSet(SPList list, string DocumentSetName)
{
SPContentType docsetCT = list.ContentTypes[“Document Set”];
Hashtable properties = new Hashtable();
properties.Add(“DocumentSetDescription”, “New Document Set”);

SPFolder parentFolder = list.RootFolder;

DocumentSet docSet = DocumentSet.Create(parentFolder, DocumentSetName, docsetCT.Id, properties, true);

return docSet;
}

[/sourcecode]

That’s it. Hope it helps you. If you need more please look at this page.

..:: I LIKE SHAREPOINT ::..

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

About Karsten Schneider 312 Articles
Consultant for Microsoft 365 Applications with a strong focus in Teams, SharePoint Online, OneDrive for Business as well as PowerPlatform with PowerApps, Flow and PowerBI. I provide Workshops for Governance & Security in Office 365 and Development of Solutions in the area of Collaboration and Teamwork based on Microsoft 365 and Azure Cloud Solutions. In his free time he tries to collect tipps and worthy experience in this blog.

2 Comments

  1. Hi I used this code But I get System.NullReferenceException

    DocumentSet docSet = DocumentSet.Create(parentFolder, DocumentSetName, docsetCT.Id, properties, true);

Leave a Reply

Your email address will not be published.


*