Create a Document Set in Code

I have a solution where we need to create custom Documents Set via code, luckily there are some good samples out there. However I ran into a strange piece of behaviour. My Document Sets were appearing as folders without Welcome Pages….

docsetpopoutmenu

I’d been using this code to create my document sets:

/*
Broken but sort of works
*/
public static void GenerateNewApplicationDocumentSet(string applicationNumber, SPList targetLibrary, SPWeb web)
{
	if (applicationNumber == null) throw new ArgumentNullException("applicationNumber");
	if (targetLibrary == null) throw new ArgumentNullException("targetLibrary");
	if (web == null) throw new ArgumentNullException("web");
	//You can use a hashtable to populate properties of the document set
	var docsetProperties = new Hashtable {{"Name", applicationNumber}};
	var documentSetContentType = web.ContentTypes[ContentTypeNames.ApplicationDocumentSet];
	documentSet = DocumentSet.Create(targetLibrary.RootFolder,
                                                    	applicationNumber,
                                                    	documentSetContentType.Id,
                                                    	docsetProperties,
                                                    	true);
}

Creating my Document Sets via the UI worked fine and gave me the right icon and a link to my Welcome Page. I was well puzzled. After a bit of head scratching and getting back to SharePoint basics I realised my mistake. I was creating an instance of the Documents Set Content Type that was on the SPWeb and not that which was bound to the Document Library.

Once I start using the content type from the library things started working perfectly!

public static void GenerateNewApplicationDocumentSet(string applicationNumber, SPList targetLibrary)
{
	if (applicationNumber == null) throw new ArgumentNullException("applicationNumber");
	if (targetLibrary == null) throw new ArgumentNullException("targetLibrary");
	//You can use a hashtable to populate properties of the document set
	var docsetProperties = new Hashtable {{"Name", applicationNumber}};
	var documentSetContentType = targetLibrary.ContentTypes[ContentTypeNames.ApplicationDocumentSet];
	documentSet = DocumentSet.Create(targetLibrary.RootFolder,
                                                    	applicationNumber,
                                                    	documentSetContentType.Id,
                                                    	docsetProperties,
                                                    	true);
}

docsets

Subtle but telling reminder that the content types bound to lists are different from those site wide content types that they inherit from.

Advertisements
This entry was posted in Development, Document Sets, SharePoint. Bookmark the permalink.

2 Responses to Create a Document Set in Code

  1. mrWoodo says:

    Nice one thanks! My problem exactly

  2. Aleksandr says:

    Thank you! This helped me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s