Share and Enjoy !

Content Type Dangers

Content Types in SharePoint are hierarchical and can even be called object oriented. Each has a parent content type such that a content type can have not only its own behaviors (e.g., workflows, information management policies) and metadata (fields), but it also inherits its parent’s behaviors and metadata.

What you may assume is that you can take the current content type and look at it’s parents recursively until the parent is null, like so:

Don’t try this at home!

DoStuff(mySpListItem.ContentType);

public void DoStuff(SPContentType contentType)
{
// Do some stuff with the content type
// ...
if (contentType.Parent != null)
{
// Recurse and do some more stuff with the parent content type
DoStuff(contentType.Parent);
}

Unfortunately, if you try the above within SharePoint server-side code you will likely cause your application pool to be terminated. Then, if you are like me, you will spend several minutes trying to figure out why SharePoint doesn’t respond .

You see, the root content type is “System” and its parent is itself (seems like this is breaking some universal law of biology). So, you will go into an infinite loop if you try the code above. Instead, try the following:

This is OK to try at home

DoStuff(mySpListItem.ContentType);
public void DoStuff(SPContentType contentType)
{
// Do some stuff with the content type
// ...
if (contentType.Parent != null && contentType.Parent.Id != contentType.Id)
{
// Recurse and do some more stuff with the parent content type
DoStuff(contentType.Parent);
}
}

Now we simply make sure that we aren’t going to continue if the parent points to ourself.

Share and Enjoy !

Related Content: