Feature Stapling allows you to create a feature and then
associate it with any site definition without ever touching the site
definition files themselves. Your feature will be activated when the
site is provisioned.
Consider a situation like you've created an awesome master page(already deployed) for your team sites but you are a bit annoyed that you have to manually set every new site to use this master page after they are created.so using Feature stapling we are going to set a default master page at the time of creating a new site itself.
To staple a Feature to a site definition, you actually need to create
another Feature that will do the stapling, and this feature is called as
Stapler Feature. A feature which is going to be stapled/associated with a site definition is called as Staplee Feature.
Consider a situation like you've created an awesome master page(already deployed) for your team sites but you are a bit annoyed that you have to manually set every new site to use this master page after they are created.so using Feature stapling we are going to set a default master page at the time of creating a new site itself.
- Create new Empty SharePoint Project.
- Right Click Feature and click Add Feature.Now you will see Feature.Temlate.Xml.(Staplee Feature)
- Right click on created Feature1 and Click add Event Receiver.
- You will Find a Event Receiver class file where you can code your own logic.
- We can now edit the feature receiver code to make it set our custom master page when the feature is activated. This is done in the FeatureActivated method. Our end result should look something like this:
- public override void FeatureActivated(SPFeatureReceiverProperties properties) { try { using (SPWeb currentWeb = properties.Feature.Parent as SPWeb) { using (SPSite currentSite = currentWeb.Site) { string masterPageUrl = string.Format("{0}/_catalogs/masterpage/CustomTeamSite.master", currentSite.ServerRelativeUrl).Replace("//", "/"); currentWeb.CustomMasterUrl = masterPageUrl; currentWeb.MasterUrl = masterPageUrl; currentWeb.Update(); } } } catch (Exception ex) { string errorMessage = string.Format("An exception occured while trying to activate the feature. Message: '{0}'.", ex.Message); throw new SPException(errorMessage); } }
- We probably want our feature to clean up after itself when it's activated, so we'll go ahead and add some code to the FeatureDeactivating method that sets the site's master page back to 'default.master'. The code looks something like this:
public override void FeatureDeactivating(SPFeatureReceiverProperties properies)
{ try { using (SPWeb currentWeb = properties.Feature.Parent as SPWeb) { using (SPSite currentSite = currentWeb.Site) { string masterPageUrl = string.Format("{0}/_catalogs/masterpage/default.master", currentSite.ServerRelativeUrl).Replace("//", "/"); currentWeb.CustomMasterUrl = masterPageUrl; currentWeb.MasterUrl = masterPageUrl; currentWeb.Update(); } } } catch (Exception ex) { string errorMessage = string.Format("An exception occured while trying to deactivate the feature. Message: '{0}'.", ex.Message); throw new SPException(errorMessage); } }
{ try { using (SPWeb currentWeb = properties.Feature.Parent as SPWeb) { using (SPSite currentSite = currentWeb.Site) { string masterPageUrl = string.Format("{0}/_catalogs/masterpage/default.master", currentSite.ServerRelativeUrl).Replace("//", "/"); currentWeb.CustomMasterUrl = masterPageUrl; currentWeb.MasterUrl = masterPageUrl; currentWeb.Update(); } } } catch (Exception ex) { string errorMessage = string.Format("An exception occured while trying to deactivate the feature. Message: '{0}'.", ex.Message); throw new SPException(errorMessage); } }
- Staplee Feature work is over the next step is to create Stapler Feature.
- So we have to create our Stapler (Feature2) which will associate Feature1 with the site definition.
- The scope of Feature2 should be Farm Level.
- Add new Empy Element "Elements" to the project.
- We will need the GUID from Feature1 . You can find this in the manifest(yellow mark in the below image).
- Edit Element.xml file in the newly added element and add follwing line,
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
FeatureSiteTemplateAssociation
TemplateName
=
"GLOBAL"
Id
=
"493a82e4-f05b-4244-91df-c99ba69db085"
></
FeatureSiteTemplateAssociation
>
<
FeatureSiteTemplateAssociation
TemplateName
=
"STS#1"
Id
=
"493a82e4-f05b-4244-91df-c99ba69db085"
></
FeatureSiteTemplateAssociation
>
</
Elements
>
- To activate the feature for type of sites in web application, we need to associate stapling a feature to global site definition(TemplateName="GLOBAL") and blank site (TemplateName="STS#1").
- Deploy the project.
9. Now whenever you create a new site using site definition specified
in FeatureSiteTemplateAssociation (ie...the TemplateName) staplee
feature will automatically get activated.
No comments:
Post a Comment