Wednesday 29 July 2015

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:-
  1. Create a site from the definition and subsequently deploy your features to the site.
  2. Include the features in the site definition (onet.xml) before using it to create sites.
  3. 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:

<Configuration ID="0" Name="BLANKINTERNET">
    <SiteFeatures>
        <Feature ID="C85E5759-F323-4EFB-B548-443D2216EFB5" />
        <Feature ID="A392DA98-270B-4e85-9769-04C0FDE267AA" />
        <Feature ID="7C637B23-06C4-472d-9A9A-7C175762C5C4" />
    </SiteFeatures>
    <WebFeatures>
        <Feature ID="00BFEA71-DE22-43B2-A848-C05709900100" />
        <Feature ID="00BFEA71-E717-4E80-AA17-D0C71B360101" />
    </WebFeatures>
    <Modules>
        <Module Name="Home" />
    </Modules>
</Configuration>

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:

<?xml version="1.0" encoding="utf-8"?>
<Solution xmlns="http://schemas.microsoft.com/sharepoint/" SolutionId="26E1A1D9-7BB2-4e76-888E-B20184B16E3B">
  <FeatureManifests>
    <FeatureManifest Location="COB.Demos.FeatureStapling\feature.xml" />
  </FeatureManifests>
</Solution>
The feature.xml file which defines the feature 'header' information:

<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="4AF9999A-0517-4224-9ED3-D2F9F87D92E2"
        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/">
  <ElementManifests>
    <ElementManifest Location="stapling.xml" />
</ElementManifests>
</Feature>

And finally, the core feature definition in the stapling.xml file:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <FeatureSiteTemplateAssociation Id="F6924D36-2FA8-4f0b-B16D-06B7250180FA" TemplateName="MySiteDefName#0" />
  <FeatureSiteTemplateAssociation Id="94C94CA6-B32F-4da9-A9E3-1F3D343D7ECB" TemplateName="MySiteDefName#0" />
  <FeatureSiteTemplateAssociation Id="02464C6A-9D07-4F30-BA04-E9035CF54392" TemplateName="MySiteDefName#0" />
</Elements>


The FeatureSiteTemplateAssociation element maps feature GUIDs to site defintions - note that the format of the TemplateName attribute value is <SiteDefName>#<ConfigurationID>. 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.



Hide Time from events in Calendar list or webpart Use below code in content edit webpart or by editing list page in SharePoint designer ...