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 ::..
Hi I used this code But I get System.NullReferenceException
DocumentSet docSet = DocumentSet.Create(parentFolder, DocumentSetName, docsetCT.Id, properties, true);
please check your parentfolder, if it is null?