Recently I had a problem with the default content types that are bind with a custom list.
I created an Announcement list and I have bind my own content type into it.Then I wanted to hide the default content type which is the “Announcement”.Initially I used the name to get the content type from the list but later I had to re factor it simply because, if we are to support multilingual we should not use names but id’s.
Then I thought this should be a piece of cake and I re factored it like bellow.
SPList list = web.Lists[listName];
SPContentType item = list.ContentTypes[SPBuiltInContentTypeId.Announcement];
item.Hidden = true;
item.Update();
list.Update();
To my amazement , as I activated the updated feature I got the error “Object reference not set to an instance of an object”.
And then I open this list from the SharePoint Manager and checked the ID of the content type “Announcement” and it is totally different one to the default content type id which is the “0x0104”.
Then I got some expert help on this and got to know that this is because what ever the bind content types in lists are a copy of the site content type.Thank you Bjorn for letting me know about it.
So here is the new code of doing it.
SPContentType item = list.ContentTypes[list.ContentTypes.BestMatch(contentTypeId)];
It was that simple once you got to know it.
So what you need to understand here is that adding a content type to a list means that you actually adding a copy of the content type.
No comments:
Post a Comment