Ads

Showing posts with label Feature. Show all posts
Showing posts with label Feature. Show all posts

Monday, 8 July 2013

SharePoint 2010 Feature Stapling

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.

Stapling basically consist of two features:
  1. Staplee Feature: The feature which will be stapled to an existing site definition
  2. Stapler Feature: The feature which will staple the staplee feature with site definition

Steps

  1. Create new Empty SharePoint Project.
  2. Create staplee feature which we want to get activated on site provisioning.
  3. Create Stapler feature with scope as Farm.
  4. Add new Empy Element "StaplerElementHelper" to the project.
  5. Edit Element.xml file
  6. Add follwing line<FeatureSiteTemplateAssociation Id="d3f566c2-b711-4ddb-808b-c4d0c00dac36" TemplateName="STS#0" />FeatureSiteTemplateAssociation is going to associate our steplee feature with site definition "STS#0"(team site).
    Note: Replace value of Id with your staplee feature ID. You can also specify your own TemplateName.

    your complete file will look like
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/"><FeatureSiteTemplateAssociation Id="d3f566c2-b711-4ddb-808b-c4d0c00dac36" TemplateName="STS#0" />
    </Elements>
  7. Double click your Stapler Feature and Include "StaplerElementHelper" in "Items in feature". Remember it should be included in Stapler feature and not in staplee feature.
  8. Deploy it.

Now whenever you create a new site using site definition specified in FeatureSiteTemplateAssociation staplee feature will automatically get activated.

Sample Project

refer click 

Wednesday, 3 April 2013

Solution Deployment/Retraction and Feature Activation/Deactivation internal process flow

When you deploy/retract SharePoint WSPs and Activate/Deactivate site collection or sub site level features at the farm level using PowerShell commands or activate/deactivate features from browser interface, SharePoint 2010 makes several changes at the different places on the servers and databases.









Lets see what really happens when you execute different farm level PowerShell commands.



Central Admin
Site/Web Level Features
GAC
Web.Config
14-RootFeatures
SharePoint Config DB, Objects Table – Solutions
SharePoint Config DB, Objects Table – Features
Web App Content DB, Features Table
Add-SPSolution
Solution Added but Not Deployed
N/A
N/A
N/A
N/A
Yes – Solution Definition Added
N/A
N/A
Install-SPSolution
Deployed to Web App
Installed Feature
Yes – DLLs Added
Yes – Safe Control Entries Added
Yes – Features Added
Yes – Solution Definition Still Exists
Yes – Feature Definition Added
N/A
Enable-SPFeature
Solution Still Deployed
Activated feature to Site/Web
Yes – Still Exists
Yes – Still Exists
Yes – Still Exists
Yes – Solution Definition Still Exists
Yes – Feature Definition Still Exists
YES – Feature Activation Link Added
Disable-SPFeature
Solution Still Deployed
Deactivated feature from Site/Web
Yes – Still Exists
Yes – Still Exists
Yes – Still Exists
Yes – Solution Definition Still Exists
Yes – Feature Definition Still Exists
NO – Feature Activation Link Deleted
UnInstall-SPFeature
Solution Still Deployed
Uninstalled feature
Yes – Still Exists
Yes – Still Exists
Yes – Still Exists
Yes – Solution Definition Still Exists
No – Feature Definition Deleted
N/A
UnInstall-SPSolution
Retracted from Web App
Uninstalled feature
No – DLLs Deleted
No – Safe Control Entries Deleted
No – Features Deleted
Yes – Solution Definition Still Exists
No – Feature Definition Deleted
N/A
Remove-SPSolution
Solution Deleted
N/A
N/A
N/A
N/A
Yes – Solution Definition Deleted
N/A
N/A


Hope this will help you to understand
Many Thanks

Introduction & steps involve in Feature Stapling in SharePoint 2010

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.

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); } }
  • 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.

Ads