Wednesday, January 14, 2009

Create and deploy a Custom Feature in Office SharePoint Server 2007

Features are a major enhancement to the template framework of MOSS 2007. It allows you to develop, test, deploy and activate custom functionality in your MOSS environment and provides a way to make the functionality available across your server farm. Examples of features are custom workflows, content types, templates of lists and libraries, etc.

Features are typically stored on the SharePoint server at C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES. Each features has its own folder and contains a header file called Feature.XML.

The Feature.XML points to a manifest that tells SharePoint where to find the code and XML that defines the feature. It contains the location of assemblies, files, dependencies or properties that support feature. Optionally, the Feature element may point to an XML file that contains an Element which defines the components and types that make up the Feature.

Creating Your Own Custom Feature
For example, i want to create a content type for lists that defines columns for storing informations about RFQ (Request For Quote).

The columns are
-RFQ No.
-Purchase Order No.
-Description
-Vendor
-RFQ Close Date

Although we can create a list template and use it inside the site, creating this content type as a Feature allows Site Administrators to make the type available to all sites in the server farm without adding the content type manually.

To create the list column definition file
1. Create a folder called fieldsRFQ to hold the files defining the feature and elements.
2. Then create the XML file inside the folder that contains the list column definitions. An easy way is duplicate the column definition file of SharePoint itself, which is located at C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES. You can copy the folder C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\fields and rename it with the new name. The folder contains two xml files, feature.xml and fieldswss.xml. I have copied and duplicated the folder with the new name "RFQColumns" and rename the file inside the folder fieldswss.xml as "fieldsrfq.xml.
3. Here is my fieldsrfq.xml, the element file that defines the columns. I have modified the copied file by defining my own column definitions.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Field ID="{2ABC0796-E9B5-4288-B8D9-F1A66B87E61C}"
Name="RFQNo"
SourceID="http://schemas.microsoft.com/sharepoint/v3"
StaticName="RFQNo"
Group="RFQ Content Type"
RowOrdinal="0"
Type="Text"
Hidden="FALSE"
DisplayName="RFQ No.">
</Field>
<Field ID="{10F319FD-87EA-413a-9C00-A8EAE1C7E3E3}"
Name="PONo"
SourceID="http://schemas.microsoft.com/sharepoint/v3"
StaticName="PONo"
Group="RFQ Content Type"
RowOrdinal="1"
Type="Text"
Hidden="FALSE"
DisplayName="Purchase Order No.">
</Field>
<Field ID="{DDD9DA23-DFE5-4de6-9D12-5231A8499998}"
Name="Description"
SourceID="http://schemas.microsoft.com/sharepoint/v3"
StaticName="Description"
Group="RFQ Content Type"
RowOrdinal="2"
Type="Text"
Hidden="FALSE"
DisplayName="Description">
</Field>
<Field ID="{0844729B-2146-4593-844E-DB5762BAEAC1}"
Name="Vendor"
SourceID="http://schemas.microsoft.com/sharepoint/v3"
StaticName="Vendor"
Group="RFQ Content Type"
RowOrdinal="3"
Type="Text"
Hidden="FALSE"
DisplayName="Vendor">
</Field>
<Field ID="{AFF26D81-88F9-433b-ACCE-F0C836F62698}"
Name="RFQCloseDate"
SourceID="http://schemas.microsoft.com/sharepoint/v3"
StaticName="RFQCloseDate"
Group="RFQ Content Type"
RowOrdinal="4"
Type="Text"
Hidden="FALSE"
DisplayName="RFQ Close Date">
</Field>
</Elements>

All the attributes in the above file is self explanatory. You have to map the sharepoint native columns against your each columns in the list.
4. Next step is to finalize the feature.xml file and paste it inside the fieldsRFQ folder. The feature XML file will look like this after the moficitation.

<?xml version="1.0" encoding="utf-8"?>
<Feature Id="3313208E-36E6-481e-BCE8-96CA777FF5F1"
Title="Request For Quote (RFQ) Columns"
Description="Installs columns designed to manage RFQ data."
Version="12.0.0.0"
Hidden="FALSE"
Scope="Site"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="fieldsrfq.xml"/>
</ElementManifests>
</Feature>

The following metadata is contained in the element in this XML file.
ID: The GUID that uniquely identifies the Feature. This value must be unique across the server farm. The easiest way to create a GUID is with the guidgen.exe utility that comes with Microsoft Visual Studio.
Title: The name of the Feature. In an actual scenario, you will see the name of the Feature displayed on the Site Features web page inside the Site Settings section for a given SharePoint site.
Description: The description of the Feature.
Version: The version of the Feature. This value can be incremented for successive versions of the Feature.
Scope:Web or Site are the typical values. The scope defines the context in which the Feature can be activated or deactivated. Setting the scope equal to Web means that the Feature can be activated or deactivated within the context of the site. A setting of Site means that the Feature can be activated or deactivated within the scope of a site collection. The scope is set to the site collection displayed to the administrator in the Site Collection Features page. There are also WebApplication and Farm values.
Hidden:TRUE or FALSE are allowable values here. This setting specifies if the Feature is visible in the list of Features on the Site Features web page. Setting the attribute to TRUE makes the Feature visible to users who want to activate it. Hidden Features must be activated either from the command line, in custom code, or through the dependency of another Feature.
DefaultResourceFile: Specifies the central location for settings and other items that may change frequently.
ImageUrl: Points to an image file that will be displayed next to the Feature in the user interface.
element: The container element for elements
element: Contains the location of the manifest file that contains the different elements that this Feature implements. The path to sub-directories and files is a relative path.

5. Now that defining the site columns for the Feature is completed, you need to create 2 files for the Feature content types that specify the components of the Feature. Create a new folder in "ctypesRFQ" folder in the path C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES. You can create the ctype.xml by duplicating the file ctypeswss.xml from C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\ctypes and the feature.xml file format remains the same. Here is my ctypeRFQ.xml.

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<ContentType ID="0x01001EF379E5-F202-434f-8DA6-3401E94E291E"
Name="RFQ Content Type"
Group="RFQs"
Description="Designed to facilitate the storage of RFQ information."
Version="0">
<FieldRefs>
<FieldRef ID="{2ABC0796-E9B5-4288-B8D9-F1A66B87E61C}" Name="RFQNo" />
<FieldRef ID="{10F319FD-87EA-413a-9C00-A8EAE1C7E3E3}" Name="PONo" />
<FieldRef ID="{DDD9DA23-DFE5-4de6-9D12-5231A8499998}" Name="Description" />
<FieldRef ID="{0844729B-2146-4593-844E-DB5762BAEAC1}" Name="Vendor" />
<FieldRef ID="{AFF26D81-88F9-433b-ACCE-F0C836F62698}" Name="RFQCloseDate" />
</FieldRefs>
</ContentType>
</Elements>

The above ctypeRFQ.xml file details the fields with GUID of the content type involved.

6. Create another file called feature.xml inside the folder ctypesRFQ and paste the code below. Please note the format is similiar to one we created before. A new GUID has to be created for the ID attribute in the file below. Also add the feature id of the content type
(one created before) in the tag as below.

<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="060EB007-9D6C-40c5-ABBE-901171CD04FF"
Title="RFQ Data Content Types"
Description="Installs content types designed to manage information about RFQ."
Version="12.0.0.0"
Hidden="FALSE"
Scope="Site"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="ctypesRFQ.xml" />
</ElementManifests>
<ActivationDependencies>
<!-- Installs the site columns that hold RFQ data -->
<ActivationDependency FeatureId="3313208E-36E6-481e-BCE8-96CA777FF5F1"/>
</ActivationDependencies>
</Feature>

7. Now we are good to go for feature deployment. Open command prompt and type cd "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN". Now execute the command in below sequence,
a) stsadm –o installfeature –filename fieldsRFQ\feature.xml
b) stsadm –o installfeature –filename ctypesRFQ\feature.xml
8. Now the feature is implemented. Next step is to activate the feature. Execute the following command in sequence below (in the same location)
a) stsadm –o activatefeature –name fieldsRFQ -url "http:\\localhost"
b) stsadm –o activatefeature –name ctypesRFQ -url "http:\\localhost"
where the URL http://localhost should be the top level site collection url where you want to deploy the feature
9. Restart the IIS

No comments: