Monday 28 December 2015

C# SharePoint Content Type Visible on New button




Problem

When using C# and the SOM API in SharePoint 2013 to add new content types to a list, you find that the new Content Types are not appearing in the "New Item" ribbon button:


 
You could go to the list settings and simply select the "visible" property for the new content types but if you are doing this as a SharePoint feature then using the UI is not an option.
 
You would expect that the SPConentType class would have a "visible in new item" property but it doesn't. There is a "hidden" property but this particular property doesn't make the new Content Types visible on the ribbon. And as they say "therein lies the problem".
 

Solution

The first thing you have to do is recover from the fact that the SharePoint 2013 SOM API does not have a "visible in new item" property (even though we know it should). So how then does the SharePoint UI do this?
 
Well I don't know because I do not have the source code to the SharePoint UI (rhetorical question).
 
After some searching I found a few articles on this topic and they suggested that "reordering the content types" would solve the problem.

I found that the UniqueContentTypeOrder collection must first be nulled then the desired content types added back into it (this isn't exactly reordering, but close enough). This is the collection in question:

 
SPList.RootFolder.UniqueContentTypeOrder
 
Doing this is effectively the equivalent of using the UI on the list settings to reorder the content types and set the Visible property: 
 
 
 So without further adieu some trial and error lead to this solution: