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.



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;
}


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.

Karsten Pohnke About Karsten Pohnke
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>

Please fill in the captcha: * Time limit is exhausted. Please reload CAPTCHA.