I decided to expand on one of Travis Nielson’s posts called Introducing Documents Sets. Being the eternal developer that I am I decided to write a blog on how to create a Document Set using C# and the new object model for SP2010.
In trying to figure out how to do this, I though I would try the 2007 way and add a new ListItem to my test document library….well that didn’t work so well. While I was looking at the code for the NewDocSet.aspx page I stumbled upon a new assembly and namespace. The assembly that I am referring to is Microsoft.Office.DocumentManagement and the namespace is Microsoft.Office.DocumentManagement.DocumentSets. Within the new namespace lives the DocumentSet class. The DocumentSet class makes it pretty easy for creating a DocumentSet. Using the same content types from Travis’ blog I created a quick console application to create the DocumentSet in code. Below are the lines of code necessary to create a DocumentSet using the default content:
SPSite site = newSPSite(“http://sp2010”);
SPWeb web = site.OpenWeb();
SPList list = web.Lists[“Proposals”];
Hashtable props = newHashtable();
props.Add(“Name”, “Proposal 1”);
props.Add(“Description”, “Proposal 1”);
props.Add(“Customer ID”, “1234”);
props.Add(“Customer Name”, “Customer 1”);
DocumentSet ds = DocumentSet.Create(list.RootFolder, “Proposal 2”, list.ContentTypes[“Proposals”].Id, props, true);
web.Dispose();
site.Dispose();
If you do not want to provision default content then after you create the DocumentSet you can add the desired content by manipulating the Item property (SPListItem) of the DocumentSet.
SPSite site = newSPSite(“http://sp2010”);
SPWeb web = site.OpenWeb();
SPList list = web.Lists[“Proposals”];
Hashtable props = newHashtable();
props.Add(“Name”, “Proposal 1”);
props.Add(“Description”, “Proposal 1”);
props.Add(“Customer ID”, “1234”);
props.Add(“Customer Name”, “Customer 1”);
DocumentSet ds = DocumentSet.Create(list.RootFolder, “Proposal 2”, list.ContentTypes[“Proposals”].Id, props, true);
web.Dispose();
site.Dispose();
If you do not want to provision default content then after you create the DocumentSet you can add the desired content by manipulating the Item property (SPListItem) of the DocumentSet.