Working with SharePoint 2010 Document Sets Programatically

SharePoint 2010 has a new concept of a Document Set, which is essentially a folder-like container allowing you to group your files in and perform various activities on the set, such as trigger workflows, submit entire set to a record center and so on.

In this article, I will focus on how you can create a new document set programatically, which is handy if you need to pre-create a collection of document sets as part of your solution deployment.

I will assume, you have a sandbox site of a Team Site template on it; in my case the URL of my sandbox site is http://intranet.contoso.com

First, we need to ensure our site has a Document Set feature enabled:

From http://intranet.contoso.com click Site Actions-> Site Settings -> Site Collection features -> ensure you have activated Document Set feature.

Now, let’s take a look at what’s involved in provisioning an instance of a document set programmatically.

1. In Visual Studio 2010, create a blanks SharePoint 2010 Solution.

2. Add a Feature to your solution structure, right click on the feature name to add an Feature Receiver.

3. Let’s add the reference to a document set assemblies in Visual Studio. Right click on References in your Visual Studio solution structure and Add a reference using a SharePoint tab.

The reference we’ll add is: Microsoft.Office.DocumentManagement

Your structure will look like this:

4. Now, switch to your Feature1.EventReceiver.cs and add the following namespace references to the class:

using System.Collections;
using Microsoft.Office.DocumentManagement.DocumentSets;

5. Replace the commented section for FeatureActivated block, with the following code:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
SPList sharedDocs = web.Lists["Shared Documents"];
if (sharedDocs != null)
{
    SPContentType docSetCT = web.ContentTypes["Document Set"];
    if (docSetCT != null)
    {
        Hashtable props = new Hashtable();
        props.Add("DocumentSetDescription", "New Doc Set");
        DocumentSet docSet = DocumentSet.Create(sharedDocs.RootFolder,
"New Doc Set", docSetCT.Id, props, true);
        docSet.Item.ProgId = "SharePoint.DocumentSet";
        docSet.Item.Update();
    }
}
}

Above, we get a hold of the site where our custom feature is activated and access the Shared Documents library on the site. Then, we ensure the Document Set content type is available on the site (it’s not going to be available if Document Set site collection was not activated, which we did activate earlier). Then, we create a new document set with the default name and description properties.

The last piece of code is assignment of ProgId. This is actually very important part. If we don’t assign ProgId, the document set will still be created but it will look like a folder and your users will be confused with the UI and other functionality, such as welcome page of the document set will be just a plain folder view.

6. Deploy the solution with Visual Studio and navigate to your Team Site’s Shared Documents library

After the set has been added with our feature receiver, the library will have in the view:

Enjoy!

This entry was posted in sharepoint 2010 and tagged , . Bookmark the permalink.

Comments are closed.