Thursday, June 12, 2008

features


Feature stapling
One aspect of features I wanted to cover quickly is the relationship SharePoint Features have with site definitions. Site definitions are created by a developer and subsequently used by site owners to create new sites. Much of this facility is also available by the 'Save site as template' functionality, though there are some minor differences. Additionally the 'Save site as template' functionality is removed on SharePoint publishing sites (or at least the link in Site Settings is via a HideCustomAction feature element), due I think, to some site information/functionality which SharePoint cannot properly save into a .stp file. So developers often create site definitions as part of their customization, and it's a process well-documented in the WSS SDK.If you have created a site definition, there are several ways of using features with it. These are:-
Create a site from the definition and subsequently deploy your features to the site.
Include the features in the site definition (onet.xml) before using it to create sites.
Use feature stapling to associate your features to the site definition.
Since the first option doesn't really associate the Features to the site definition, we'll focus on options 2 and 3.
2. Include features in site definition
This option should be used if the site definition has not yet been deployed, and no sites have yet been created from it. The section of the onet.xml file where features are associated with the site definition contains may look something like:{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs20 \cf2 <\cf13 Configuration\cf2 \cf6 ID\cf2 =\cf0 "\cf2 0\cf0 "\cf2 \cf6 Name\cf2 =\cf0 "\cf2 BLANKINTERNET\cf0 "\cf2 >\par ?? <\cf13 SiteFeatures\cf2 >\par ?? <\cf13 Feature\cf2 \cf6 ID\cf2 =\cf0 "\cf2 C85E5759-F323-4EFB-B548-443D2216EFB5\cf0 "\cf2 />\par ?? <\cf13 Feature\cf2 \cf6 ID\cf2 =\cf0 "\cf2 A392DA98-270B-4e85-9769-04C0FDE267AA\cf0 "\cf2 />\par ?? <\cf13 Feature\cf2 \cf6 ID\cf2 =\cf0 "\cf2 7C637B23-06C4-472d-9A9A-7C175762C5C4\cf0 "\cf2 />\par ?? \par ?? <\cf13 WebFeatures\cf2 >\par ?? <\cf13 Feature\cf2 \cf6 ID\cf2 =\cf0 "\cf2 00BFEA71-DE22-43B2-A848-C05709900100\cf0 "\cf2 /> \par ?? <\cf13 Feature\cf2 \cf6 ID\cf2 =\cf0 "\cf2 00BFEA71-E717-4E80-AA17-D0C71B360101\cf0 "\cf2 /> \par ?? \par ?? <\cf13 Modules\cf2 >\par ?? <\cf13 Module\cf2 \cf6 Name\cf2 =\cf0 "\cf2 Home\cf0 "\cf2 />\par ?? \par ??}-->














The ID value is, of course, the feature GUID. The SiteFeatures element contains features which should be activated when the site definition is used to create a site collection, and the WebFeatures element contains features for when the definition is used to create a standard web within a site collection.
3. Use feature-stapling to associate your features to the site definition
This option should be used when the site definition is already in use (and sites have been created). This is because there are risks in modifying a site definition once it has been deployed, since the site definition files on the filesystem are used for pages which are ghosted (not modified).
To use feature-stapling, you first need the feature(s) you wish to link to the site definition - these should be created in the normal way. The, you should create a 2nd feature which does the actual stapling. Sample values for the various files are shown below:
The manifest.xml file if you are wrapping your feature in a solution:{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs20 \cf2 \par ??<\cf13 Solution\cf2 \cf6 xmlns\cf2 =\cf0 "\cf2 http://schemas.microsoft.com/sharepoint/\cf0 "\cf2 \cf6 SolutionId\cf2 =\cf0 "\cf2 26E1A1D9-7BB2-4e76-888E-B20184B16E3B\cf0 "\cf2 >\par ?? <\cf13 FeatureManifests\cf2 >\par ?? <\cf13 FeatureManifest\cf2 \cf6 Location\cf2 =\cf0 "\cf2 COB.Demos.FeatureStapling\\feature.xml\cf0 "\cf2 />\par ?? \par ??}-->






The feature.xml file which defines the feature 'header' information:{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs20 \cf2 \par ??<\cf13 Feature\cf2 \cf6 Id\cf2 =\cf0 "\cf2 4AF9999A-0517-4224-9ED3-D2F9F87D92E2\cf0 "\cf2 \par ?? \cf6 Title\cf2 =\cf0 "\cf2 COB.Demos.FeatureStapling\cf0 "\cf2 \par ?? \cf6 Description\cf2 =\cf0 "\cf2 Staples a set of features to custom site definitions. The features/site definitions are defined in stapling.xml.\cf0 "\cf2 \par ?? \cf6 Version\cf2 =\cf0 "\cf2 1.0.0.0\cf0 "\cf2 \par ?? \cf6 Scope\cf2 =\cf0 "\cf2 Farm\cf0 "\cf2 \par ?? \cf6 Hidden\cf2 =\cf0 "\cf2 FALSE\cf0 "\cf2 \par ?? \cf6 xmlns\cf2 =\cf0 "\cf2 http://schemas.microsoft.com/sharepoint/\cf0 "\cf2 >\par ?? <\cf13 ElementManifests\cf2 >\par ?? <\cf13 ElementManifest\cf2 \cf6 Location\cf2 =\cf0 "\cf2 stapling.xml\cf0 "\cf2 />\par ??\par ??}-->

Title="COB.Demos.FeatureStapling"
Description="Staples a set of features to custom site definitions. The features/site definitions are defined in stapling.xml."
Version="1.0.0.0"
Scope="Farm"
Hidden="FALSE"
xmlns="http://schemas.microsoft.com/sharepoint/">



And finally, the core feature definition in the stapling.xml file:{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0??;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;??\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;??\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;??\red192\green192\blue192;}??\fs20 \cf2 <\cf13 Elements\cf2 \cf6 xmlns\cf2 =\cf0 "\cf2 http://schemas.microsoft.com/sharepoint/\cf0 "\cf2 >\par ?? <\cf13 FeatureSiteTemplateAssociation\cf2 \cf6 Id\cf2 =\cf0 "\cf2 F6924D36-2FA8-4f0b-B16D-06B7250180FA\cf0 "\cf2 \cf6 TemplateName\cf2 =\cf0 "\cf2 MySiteDefName#0\cf0 "\cf2 />\par ?? <\cf13 FeatureSiteTemplateAssociation\cf2 \cf6 Id\cf2 =\cf0 "\cf2 94C94CA6-B32F-4da9-A9E3-1F3D343D7ECB\cf0 "\cf2 \cf6 TemplateName\cf2 =\cf0 "\cf2 MySiteDefName#0\cf0 "\cf2 />\par ?? <\cf13 FeatureSiteTemplateAssociation\cf2 \cf6 Id\cf2 =\cf0 "\cf2 02464C6A-9D07-4F30-BA04-E9035CF54392\cf0 "\cf2 \cf6 TemplateName\cf2 =\cf0 "\cf2 MySiteDefName#0\cf0 "\cf2 />\par ??}-->





The FeatureSiteTemplateAssociation element maps feature GUIDs to site defintions - note that the format of the TemplateName attribute value is #. This obviously allows a degree of flexibility and allows you to do fairly complex things with different configurations of site definitions.The final thing to note is that feature-stapling won't affect any sites which have already been created from a site definition, only subsequent sites. The only way to do this is to activate your features individually on the site, though obviously an STSADM script can help you activate multiple features against multiple sites

No comments: