Friday, April 19, 2013

Create document set programmatically


In this post we are going to see how to create a document set in SharePoint programmatically.

To start with we need to activate the feature called document set at the site collection level.

If you would like to know more about document set, then I would recommend you reading Understand Document Set to get a fair idea on what document set is and what it offers.

I have one document library with the name Shared Document. To add document set programmatically, we first need to enable the content types on this library. So go to the library settings, advance settings and enable the allow management of content types.

Now click on add from existing content types.

On the page look for document set content type and add it to the library.

So you should have something like this



Now I am showing this with an example of feature activated event. I have one feature and on activating the feature I am creating a document set programmatically.

Here is a code that you need to write.



Now if you see what we have done here is first we have taken document set content type in to a code from current web. This will not work if you have not activated document set feature at a site collection level.

Document set has properties, so we have set one property - document set description. This needs to be done with HashTable.

Then we are creating a document set by DocumentSet.Create method. To get this done you need to add reference to one DLL and for the HashTable you need to one DLL.

These are the DLLs you need to refer.

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

After creating Document Set, the important line of code to note is the ProgId of the item to be set to SharePoint.DocumentSet.

If you do not do this, then all you get is just a folder and not actual document set. You can see the difference in the icon as well as when you click on the folder.

Finally we update the item.

So let's deploy the WSP and activate the feature and see what happens.



See the icon. Icon indicates that this is the document set and not the plain folder. Click on the title.



See the description is also set. You can also set additional properties at the time of creating document set.

I hope this helps.


No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com