PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

How to create a Custom Content Type with a feature

Posted by PANVEGA on January 14, 2009

In this post I am gonna show you in only a few steps and a short introduction how easy it is to create a Custom Content Type  for a SP Feature Deployment.

Introduction:

A Content Type is a SharePoint object that is closely associated to List item (objects that inherit from SPItem), these objects host meta-data information describing the item it’s associated with. In Windows SharePoint Services, a content type is a reusable collection of settings that are applied to a certain category of content. Learn how to build a custom content type by using Microsoft Visual Studio 2005 Extensions for Windows SharePoint Services 3.0.

A Content Type is a definition of the metadata and behavior of a SharePoint list item. In short, it specifies a type of list item based on:

  • The columns that compose the list item;
  • The document template used to create a new item (if it’s a document content type);
  • The information management policies associated with it;
  • The workflows associated with it.

Content types have a hierarchic structure, which means that each content type has a parent content type from which it inherits all the definitions. These definitions can be overriden in the child content type.

Each list can have one or more associated content types. Each content type associated to a specific list is called List Content Type. However, any List Content Type must be based on a Site Content Type, which is a content type definition that is not associated to a specific list.

A Site Content Type exists in the context of a web site (organized in groups, in the site content type gallery) and can be associated with any lists of that web site or child web sites.

One can create new site content types or edit the existing ones following the steps below:

  • Site Actions (press the button)
  • Site Settings
  • Site Content Types (in the Galleries option group)

Creating a content type features become very important when it’s time to take your solution and use it with a different site collection/web application. One of the powers of the SharePoint “technology” lays in it’s ability to support reusable components, and this is something I personally use day in and day out.

That is enough for the beginning, let us start to create our own Fetaure step by  step without any VSTO support.

1. Let’s first create a feature folder named “CustomContentType”, this folder will host all of the components that make up the feature.

2. Now that we have our folder let’s create a “feature.xml” file, this file will define our feature and contain references to all components used in our content-type.

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/&#8221;
Id=”D8BF66E1-FC63-4c40-831F-B89D5F3FED13″
Title=”Rotating Banner Content Type”
Description=”Custom Content Type Feature for the Banner CQWP”
Scope=”Site” Version=”1.0.0.0″>
<ElementManifests>
<ElementManifest Location=”columns.xml” />
<ElementManifest Location=”ctypes.xml” />
</ElementManifests>
</Feature>

Notes about this feature manifest:

  • The title of the feature is Rotating Banner Content Type.
  • It will be deployed as a Site Collection feature, since it’s Scope value is Site.
  • It references to the  ctypes.xml and columns.xml in order to define our necessary fields

Each of the attributes should be unique to your feature and shouldn’t conflict with any existing feature WSS/MOSS out of the box.

If you choose Scope=”Web” instead of minimum Site you receive the following error message when deploying the feature. The feature will be deployed globally.

Elements of type ‘Field’ are not supported at the ‘Web’ scope. This feature could not be installed.

3.  Create two xml files in your CustomContentTypes folder, “ctypes.xml” and “columns.xml”. The “ctypes.xml” file will define our content-types and contain references to the fields we create in our “columns.xml” file. The “columns.xml” file will define the fields and columns available for our contenttypes.

4. In our “ctypes.xml” file copy the following xml:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;
<ContentType ID=”0x0100A3B0A8BC17A149e8BEB2EE9E30F993DC”
Name=”Banner Type”
Group=”Banner Types”
Description=”Banner Content Type.”
Version=”1″ >
<FieldRefs>
<FieldRef ID=”{fa564e0f-0c70-4ab9-b863-0177e6ddd247}” Name=”Title” Required=”TRUE”/>
<FieldRef ID=”{CA03227B-77AE-4617-BDFA-FB91299AF318}” Name=”BannerLink” Required=”TRUE” />
<FieldRef ID=”{FCC58C53-9EDF-45c7-97CF-CF336D670EAA}” Name=”BannerNotes” Required=”TRUE” />
</FieldRefs>
</ContentType>
</Elements>

For our Content Type I wanna have a List Item 0x01 with a Title, which already exists in WSS and 2 new fields BannerLink and BannerNotes. All fields shoud be mandatory columns. The FieldRefs element of the content type contains the field references to the fields you defined in the Fields element.

The “Elements” element only contains an “xmlns” attribute which isn’t anything too tricky to figure out, but let’s talk about the “ContentType” element.

Content Type IDs

The “Id” attribute is a concatenation of a reference to it’s parent(s) content-type(s) and it’s own unique identifier. All content-types must inherit from “system” and “item”, this is a very important detail. The “Id” for system is “0x0” and the “uniqId” for item is “1”, so all content-types must begin with “0x01”. As long as the content-type inherits from system and item, we can add our unique identifier on to the end of that forming our content-types Id. Here 0x01 is for a item where adding 00 + GUID = 0x0100A3B0A8BC17A149e8BEB2EE9E30F993DC

contenttypeid

SharePoint employs a special numbering scheme for identifying each content type, which it uses as a shortcut for creating new content type instances. Without such a numbering scheme, it might have been prohibitive to enable content type inheritance, since SharePoint would have needed to search through the database trying to resolve content type dependencies. This way, it only needs to examine the identifier, which it reads from right to left. For example, the Picture content type identifier is 0x010102, which SharePoint reads as id 02 (Picture) derived from Document (0x0101) derived from Item (0x01) derived from System (0x).

For custom content types that you define yourself, the identifier includes a suffix, which is the GUID associated with our type, separated by 00 as a delimiter. For example, look at an example content type id 0x0101004A257CD7888D4E8BAEA35AFCDFDEA58C. Again, reading from right to left, you have 4A257CD7888D4E8BAEA35AFCDFDEA58C derived from Document (0x0101) derived from Item (0x01) derived from System (0x). The 00 serves as a delimiter between the GUID and the rest of the identifier, as shown below.

contenttypeid2

learn more: http://msdn.microsoft.com/en-us/library/aa543822.aspx

The “FieldRef” element, which is wrapped by the “FieldRefs” element, contains attributes referencing directly to the fields used within our content-type. The “ID” attribute references a unique ID of the Field that we wish and the name reference the name of that Field. This is fairly straight forward. The fields that we are referencing are all custom but could very well be an already existing field (you can find existing fields in the feature directory of the MOSS 12 hive).

  • ID – The ID of the new content type. This ID bears information about the content type hierarchy and, for that reason, cannot be randomly generated. More about this below.
  • Name – The name of the content type.
  • Description – (optional) A descriptive text about the content type.
  • ReadOnly – (optional) Specifies if the content type is read-only, which means its definition cannot be changed. The default value is FALSE.
  • Hidden – (optional) Specifies if the content type is hidden, which means SharePoint will not show it in the New button in the list toolbar. The default value is FALSE.
  • Sealed – (optional) Specifies if the content type is sealed, which means that it cannot have child content types. Only a site collection administrator can unseal a content type. The default value is FALSE.
  • FeatureId – (optional) ID of the feature in which the content type is included.
  • Group – (optional) Name of the content type group to which this content type is assigned.

The <ContentType> element has three optional child elements, one of which is shown in the sample above: the <FieldRefs> element. The other two are <XmlDocuments> and <DocumentTemplate>.

The FieldRefs Element

The <FieldRefs> element is used to specify which site columns are part of the site content type. This element has two possible child elements: <FieldRef> and <RemoveFieldRef>.

The <FieldRef> element specifies a site column that belongs to the content type, and <RemoveFieldRef> specifies a site column that belongs to the parent content type but will be removed from this content type. Both these elements have a mandatory ID attribute that uniquely identify the site column and can have several additional optional attributes (which are a subset of those presented in the previous post for the <Field>; element).

After the FieldRefs Tag you can add something like this in order to apply your custom Form aspx file.

<XmlDocuments>
    <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
      <FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
        <Display>DocumentLibraryForm</Display>
        <Edit>DocumentLibraryForm</Edit>
        <New>DocumentLibraryForm</New>
      </FormTemplates>
    </XmlDocument>
  </XmlDocuments>

5. Copy the following xml into the “columns.xml” file:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/&#8221; >
<Field ID=”{CA03227B-77AE-4617-BDFA-FB91299AF318}”
Name=”BannerLink”
StaticName=”BannerLink”
SourceID=”http://schemas.microsoft.com/sharepoint/v3&#8243;
Group=”Banner Types”   DisplayName=”BannerLink”
Type=”Text”    Format=”FALSE”
Required=”True”     Sealed=”TRUE”/>
<Field ID=”{FCC58C53-9EDF-45c7-97CF-CF336D670EAA}”
Name=”BannerNotes”
StaticName=”BannerNotes”
SourceID=”http://schemas.microsoft.com/sharepoint/v3&#8243;
Group=”Banner Types”   DisplayName=”BannerNotes”
Type=”Note”   NumLines=”6″
Format=”FALSE”    Required=”True”   Sealed=”TRUE”/>
</Elements>

Above you see only 2 Field IDs why?  Beacause I wanna use the already existing WSS Title column. So instead of  referencing all fields you only need to add your new fields. However you have to know the ID of the Title here  fa564e0f-0c70-4ab9-b863-0177e6ddd247 . It is always the same ID.The second filed is from type Note with 6 Lines. You can also add as a RichTextField, too.

Now we see something familiar to what we saw in our “ctypes.xml” file, you’ll notice that some of those “Id” attributes look familiar… in fact, they’re the same Id’s! In this file we define each of the fields referenced by our content-type. The “Field” element contains a few attributes that we need to create our custom fields:

ID (guid, unique identifier for our field)
Name (the internal name of the field)
StaticName (the static name field)
SourceID (a reference to the sharepoint v3 namespace)
Group (the group where the column will appear
DisplayName (the name that appears in our content-type)
Type (the data-type of the field i.e text, number, lookup)
Format (the format that thefield is presented)
Required (boolean, whether the field is required in our content-type)
Sealed (see schema at the end)

6. Now that it’s ready, save all the files and copy it into the “Feature” directory of the 12 hive (12/Template/Features).

Once copied, open up the “stsadm” command-line tool and enter the following commands:

stsadm -o installfeature -name CustomContentTypes -force
I would recommend you to create a SP Solution with a .wsp file and deploy everything in a whole package to your MOSS application in a powerful way

See this post: https://panvega.wordpress.com/2009/01/14/how-to-create-a-solution-for-sharepoint

That’s it! Now your feature is waiting to be activated on the site of your choice. Once activated, you have your own brand-new custom content-type.

If you wonna bind a PageLayout Content Type like Welcome.aspx the follow these links:

http://www.sharepointdevwiki.com/display/public/ContentTypeBinding

http://www.sharepointbrainfreeze.com/archive/2007/11/27/creating-a-default-custom-moss-publishing-page-feature.aspx

Here is a nice tool for creating Content Types and fields:

http://sharepoint-dms.com/blog/Lists/Posts/Post.aspx?ID=17

More useful links:

http://msdn.microsoft.com/en-us/library/ms472236.aspx

http://msdn.microsoft.com/en-us/library/cc161067.aspx

http://msdn.microsoft.com/en-us/library/aa543225.aspx

http://www.u2u.info/Blogs/karine/Lists/Categories/Category.aspx?Name=SharePoint%202007

Advertisements

2 Responses to “How to create a Custom Content Type with a feature”

  1. Frantisek Urbanek said

    Why it is not possible to create Fields with “web” scope? It is possible from the UI. Simply create a new web, go to the web, then Site Action – Site Settings – Site Columns and crate a new column. The source now will not me the root web (site), but newly created web. Why it is not possible to the same using features?

    Thanks a lot!

  2. vijay said

    hi,

    How can i deploy a feature which works for all libraries, i tried specifying content type id as “0x0101” in elements.xml of the feature but it only picks document library.

    Can you please suggest as why the same feature doesnt get reflected for picture library?

    Thanks

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

 
%d bloggers like this: