Tuesday, February 19, 2008

Always use CAPS in content type IDs

Content type ID in the typical form looks like 0x0100[GUID], where the part shown in bold is ID of a parent content type, 00 is a separator, and [GUID] is a GUID which makes content type ID unique. A good practice is to place GUID in CAPS only. You should always do it only this way! I'll show you why it is so important in a simple example.

Consider the following: you have a content type Test Item and you need to add a button Test Action to its display form, which links to some other page. To solve this problem, you will probably create a feature, which will deploy a content type and a custom action. Let's imagine that you will define your content type without dealing with this CAPS in GUID rule:

<ContentType
ID="0x0100EB06FD49D1764bc28CFE4F0971356D39"
Name="Test Item"
Group="Test Content Types"
Description=""
Version="0"
Hidden="FALSE"
ReadOnly="FALSE"
Sealed="FALSE">
<FieldRefs>
</FieldRefs>
</ContentType>
The custom action will be defined as:
<CustomAction 
Id="TestCustomActions.DisplayFormToolbarAction"
RegistrationType="ContentType"
RegistrationId=
"0x0100EB06FD49D1764bc28CFE4F0971356D39"
Location="DisplayFormToolbar"
Sequence="100"
Title="Test Action">
<UrlAction Url="#"/>
</CustomAction>
After deploying and activating this feature, you will see a strange issue - there will be no Test Action button in the display form of Test Item. To solve this issue, you should replace content type ID in both definitions (content type and custom action) with 0x0100EB06FD49D1764BC28CFE4F0971356D39. After redeploying the feature, this issue will be solved. That's why CAPS rule makes sense!