Mariusz, Gorzoch tech Blog

SPList – where is my Content type ?

leave a comment »

Today I crossby to strange issue with looking for an content type inside the list. List was set-up in the way that it contain the content type I was looking for and it should return it properly. What was strange for me was that when I was looking for my content type inside list content types by providing it Id, then list always returned that this content type can not be found(null value). As it was so strange for me, so I go and debug the code and found that the content type I’m looking for exists in list content types, but it uses diffrent id. Diffrence of this id was that the oryginal content type id was extended by adding list instsance id on the end of content type id. This looks strange so I dig for a while and then I found, that the reason for that is that each time when you add content type to particular list, then SharePoint is creating a copy of this entire content type and add it to the list with new content type copy id. After I found that evertyhink started to make sense.
On the end to find my content type I need to use code shown bellow:

SPWeb web = properties.OpenWeb();
SPList list = web.Lists[properties.ListId];
string OriginalCTId = "0x010100BEC52D84430640B2AB6192B72E9CC497";

SPContentTypeId ctId = new SPContentTypeId(OriginalCTId);

//magic goes hire 😉
SPContentTypeId listCtId = list.ContentTypes.BestMatch(ctId);
if (listCtId == null) throw new Exception(String.Format("Content type can not be found inside list ‘{0}’.",list.Title));

SPContentType FolderYM = list.ContentTypes[listCtId];
//make sure that proper CT was found
if (FolderYM.Parent.Id != ctId) throw new Exception(String.Format("Content type can not be found inside list ‘{0}’.",list.Title));

so if you stuck with looking for your CT added to the list… above you have answer what to do to find it.


Written by Mariusz Gorzoch

20 May 2009 at 13:01

Posted in SharePoint

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: