Second Life of a Hungarian SharePoint Geek

December 12, 2009

Checking the existence of a content type

Filed under: Content types, Reflection, SharePoint — Tags: , , — Peter Holpar @ 02:38

In my previous post I wrote about an interesting issue having with content types.

One thing I’ve learned from that issue is that if we want to check if the new content type can be added to the collection, it is not enough to check

contentTypes["MyContentType"] == null

we should be prepared to handle issues, when the content type is defined on another level of the site structure. You can handle that situation reactively by catching the exception, or proactively by calling the Exists method as illustrated on the example in the former post.

You can encapsulate the functionality into your custom method:

  1. public bool ContentTypeExists(SPContentTypeCollection contentTypes, SPContentType contentType)
  2. {
  3.     bool result = false;
  4.     SPContentType contentTypeOut = null;
  5.     Type typeSPContentTypeCollection = contentTypes.GetType();
  6.     MethodInfo miExists = typeSPContentTypeCollection.GetMethod("Exists", BindingFlags.NonPublic | BindingFlags.Instance);
  7.     object[] methodParms = new object[] { contentType, true, contentTypeOut };
  8.     try
  9.     {
  10.         result = (bool)miExists.Invoke(contentTypes, methodParms);
  11.     }
  12.     catch (Exception ex)
  13.     {
  14.         // log the error
  15.     }
  16.     return result;
  17. }

Again, don’t forget to reference System.Reflection from your code!

So what does Exists do exactly? If you call that with the second parameter set to false, then it checks only the current content type collection for content types having the same Id as the one you passed as the first parameter. Since new content types has empty Id until you add them to a collection, it is pretty useless for us right now.

If you call the Exists with the second parameter set to true, then it first does the check described above, but then it checks for content type having the same Name as the one you passed as the first parameter, and last it checks for content type having the same name in the AvailableContentTypes collection of the SPWeb instance the content type collection belongs to.

Next question comes: what is AvailableContentTypes collection and how does it differ from the standard ContentTypes collection?

If you check the source using Reflector, you can see that there is not so much difference in the code, both property getter calls this internal constructor of SPContentTypeCollection:

internal SPContentTypeCollection(SPWeb web, bool bAll)

Both property getters pass the current SPWeb instance as the first parameter. The single, but significant difference is in the second parameter. ContentTypes call the constructor with false as the second parameter, while AvailableContentTypes calls with true.

It has the consequence, that in the private FetchCollection method of the SPContentTypeCollection class the stored procedure proc_ListContentTypesInWeb is called for ContentTypes and proc_ListContentTypesInScope is called for AvailableContentTypes. The first one reads up only the content type defined in the scope. Scope means the ServerRelativeUrl property of the SPWeb, so it will have the effect of reading up all content type defined in the current web and in all of the parent webs.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

Create a free website or blog at

%d bloggers like this: