Content type added to a list does not appear on the New Menu

I’m not sure if others have encountered this behaviour when progammatically adding a content type to a list.

In the following code I am adding a content type to a list and ensuring that it appears on the new menu, or am I?.

// make sure the list allows content type changes
destList.ContentTypesEnabled = true;
destList.Update();
//Add the new content type
destCT = destList.ContentTypes.Add(destWeb.AvailableContentTypes[contentType]);
destList.Update();
//blat the list of content type on the new button
destList.RootFolder.UniqueContentTypeOrder = null;
destList.RootFolder.Update();
//Get a list of content types for the 'new' drop down on the list
//exclude the folder
List<SPContentType> cTypes = destList.ContentTypes.Cast<SPContentType>().Where(ct => ct.Name != "Folder").ToList();
//Set the content types for the 'new' drop down list
destList.RootFolder.UniqueContentTypeOrder = cTypes.ToArray();
destList.RootFolder.Update();

It does not seem to work. The content type is now in the list but it does not appear on the new menu. I had to modify the code as follows.

// make sure the list allows content type changes
destList.ContentTypesEnabled = true;
destList.Update();
//Add the new content type
destCT = destList.ContentTypes.Add(destWeb.AvailableContentTypes[contentType]);
destList.Update();
//blat the list of content type on the new button
destList.RootFolder.UniqueContentTypeOrder = null;
destList.RootFolder.Update();
//Get a list of content types for the 'new' drop down on the list
//exclude the folder
List<SPContentType> cTypes = destList.ContentTypes.Cast<SPContentType>().Where(ct => ct.Name != "Folder").ToList();
if (destCT != null && !cTypes.Contains(destCT))
 cTypes.Add(destCT);
//Set the content types for the 'new' drop down list
destList.RootFolder.UniqueContentTypeOrder = cTypes.ToArray();
destList.RootFolder.Update();

Don’t be tempted to add the content type twice, as can be seen in the following code snippet.

// make sure the list allows content type changes
destList.ContentTypesEnabled = true;
destList.Update();
//if the content type has not taken try again
if (destList.ContentTypes[contentType] == null)
{
 destCT = destList.ContentTypes.Add(destWeb.AvailableContentTypes[contentType]);
 destList.Update();
}
//blat the list of content type on the new button
destList.RootFolder.UniqueContentTypeOrder = null;
destList.RootFolder.Update();
//Get a list of content types for the 'new' drop down on the list
//exclude the folder
List<SPContentType> cTypes = destList.ContentTypes.Cast<SPContentType>().Where(ct => ct.Name != "Folder").ToList();
//Set the content types for the 'new' drop down list
destList.RootFolder.UniqueContentTypeOrder = cTypes.ToArray();
destList.RootFolder.Update();

This would appear to work but there is a hidden problem. Take a look at the content types registered on the list and you might notice that all content type are associated with all of the columns registered with individual content types. Not nice :cry:

I would be interested to hear from anybody who has encountered this behaviour.  I am still not convinced and feel that I may simply be adding the content type incorrectly.

This entry was posted in .NET Development, SharePoint, SharePoint 2007, SharePoint 2010 and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>