Thursday, December 31, 2009

Custom Feature to Create a Content Type : Part 2

1) Create a new project (class Library) and then we will be creating a directory structure where we will create a directory called MyContentType which we will be deploying to the SharePoint feature folder. [Hence we are mimicking or shadowing the directory structure as is present in the SharePoint 12 hive].

2) Then we will create a XML file called feature.xml which contains a Feature element and it defines a feature and specifies the location of assemblies, files, dependencies, or properties that support the Feature.


< Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Id="08A7F171-A9A2-4a9b-8CC2-E1686C6F35A2"
Title="My First Content Types"
Description=" Content Type of the Site"
Version="1.0.0.0" Scope="Site" Hidden="False" >
< elementmanifests >
< elementmanifest location="MyContentType.xml" >
< elementmanifest location="MyContentTypeColumn.xml" >
</elementmanifests >
</feature >


The feature element is the root element which contains the following attributes:
Id (a guid)
Title (Title of the feature)
description (description of the feature)
version (1.0.0.0, current feature version)
Scope (web, site, web application, farm) for content type we have to keep at Site.
xmlns (http://schemas.microsoft.com/sharepoint/, generally just the SharePoint namespace).

Notice the "ElementManifest" elements, these reference separate xml files "MyContentType.xml" and "MyContentTypeColumn.xml" which have yet to be created. These referenced xml files will contain field/column references that will be used in our content-type.

3) Create two xml files in your MyContentType folder, "MyContentType.xml" and “MyContentTypeColumn.xml". The "MyContentType.xml" file will define our content-types and contain references to the fields we create in our "MyContentTypeColumn.xml" file. The "MyContentTypeColumn.xml" file will define the fields and columns available for our content types.

4) First we will create a top level element called Elements inside which we will be adding the element called ContentType

< Elements xmlns=http://schemas.microsoft.com/sharepoint/ >
< ContentType ID="0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF390053F95050258F46abA22C328B17956A00"
Name="MyContent" Group="Custom Content Types"
Description="Custom Content type designed by Hemant"
Hidden="FALSE" >
< FieldRefs >
< FieldRef ID ="{7FE28353-9609-455d-B716-6E32E98084E5}" Name="MyHobby"/>
< FieldRef ID ="{B6014DB3-B39E-4ef2-8924-AF209F099BFD}" Name="MyBestMovie"/>
</FieldRefs>
</ContentType>
</Elements>


The "ContentType" element contains a few attributes:
Name (name of ContentType)
Group (group the ContentType belongs to)
Description (description of the ContentType)
Version(version of ContentType)

The "Id" attribute is a concatenation of a reference to its parent(s) content-type(s) and its own unique identifier. Our content-types inherit from the Publishing Content Types called Page who’s Id is underlined as shown above and then we add our own unique Id.

The "FieldRef" element, which is wrapped by the "FieldRefs" element, contains attributes referencing directly to the fields used within our content-type. The "ID" attributes references a unique ID of the Field that we wish and the name reference the name of that Field. 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).

5) Again we will be creating an XML file called Elements.xml named “MyContentTypeColumn.xml" In this file we define each of the fields referenced by our content-type in the previous xml file "MyContentType.xml”. 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)

< Elements xmlns="
http://schemas.microsoft.com/sharepoint/" >
< Field ID ="{7FE28353-9609-455d-B716-6E32E98084E5}" Name="MyHobby"
Type="HTML" Title="MyHobby"
DisplayName="MyHobby" RichTextMode="FullHtml"
Group="MyCustomTypes" StaticName="MyHobby" RichText="TRUE" >
</Field >
<Field ID ="{B6014DB3-B39E-4ef2-8924-AF209F099BFD}" Name="MyBestMovie"
Type="HTML" Title="MyBestMovie"
DisplayName="MyBestMovie" RichTextMode="FullHtml"
Group="MyCustomTypes" StaticName="MyBestMovie" RichText="TRUE" >
</Field >
</Elements >


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

7) Now feature is waiting to be activated on the site of your choice, it can be activated from the command line or through the “Modify all Site Settings” in Site Action.

stsadm -o activatefeature -filename MyCustomContentTask Feature.xml -URL
http://localhost:82/

No comments:

Post a Comment