开发者

Detecting custom folder content types in MOSS2007

Given an SPListItem representing a folder, I need to fi开发者_如何学Cnd 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?


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>.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜