views:

139

answers:

2

Given an SPListItem representing a folder, I need to find out whether it has the builtin folder content type, or a custom folder content type (with additional fields).

Here is what I do

    SPContentType folderType = aFolderItem.Web.AvailableContentTypes[SPBuiltInContentTypeId.Folder];
    SPContentType contentType = aFolderItem.ContentType;

    if ( contentType != folderType )
       ...

However, for default folders I still get a different content type than the built in type. They have IDs like 0x0120007C34D9760794FA43AB267F4E1A1BF460 . I'm not sure where this particular GUID suffix comes from, I can't find it in any of the definition of my \features folder.

My best guess is that instantiated folders always get a custom type. If that's the case, any suggestion on how I might be able to differentiate between builtin folders and custom folders?

+1  A: 

If a content type is attached to a list, then it will not be attached directly but a copy of it will be created and attached. The original content type id is then annexed with a Guid and becomes the "new" content type's id.

If you kept to the guide lines for defining content type ids, then you custom folder content type id should look something like 0x0120 + 00 + <Guid>.

So if you check an item's content type whether it is a default or a custom folder, the id of a custom folder will look something like 0x0120 + 00 + <Guid> ==> Base folder content type id + a Guid for attaching it to a list.

In contrast to that the id of a custom folder will look something like 0x0120 + 00 + <Guid> + <Guid> ==> Base folder content type + a Guid for your custom content type + a Guid for attaching it to a list.

To make the comparison a little bit easier you should not compare the actual content type's id but have a look at the content type id's parent id. For a custom folder the parents id is 0x0120, for a custom folder it will be 0x0120 + 00 + <Guid>.

Flo
Aha, thanks for confirming that is the way things work. Now that I thought a little bit more about it, it's actually kinda obvious (seeing how list content types can have customizations that are separate from the parent site content type). Your suggestion on how to detect (applied) builtin folders should work like a charm. Excellent.
Paul-Jan
A: 

Assuming that aFolderItem is the Variable of type SPListItem below code should get what you want.

if(aFolderItem.ContentType.Id.IsChildOf(SPBuiltInContentTypeId.Folder))
          ...this is a Folder
else
          ...this is not a Folder
Kusek
Agreed, but I want to see the difference between BUILTIN folders and CUSTOM folders. Both will definitely be a child of SPBuiltInContentTypeId.Folder .
Paul-Jan
I.e. not a direct child, but a descendant.
Paul-Jan