Quantcast
Channel: Gav's Blog – SharePoint .NET & Tech
Viewing all articles
Browse latest Browse all 41

Create a Document Set in Code

$
0
0

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.


Filed under: Development, Document Sets, SharePoint

Viewing all articles
Browse latest Browse all 41

Trending Articles