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
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.