Creating a site feature to apply branding

SharePoint features provide a mechanism to add our custom branding elements to the SharePoint farm at four different scopes. Features can be scoped to the farm level, web application level, site collection level, or site (web) level. In addition to adding content, they can run custom code when activated, upgraded, deactivated, and so on in feature event receivers.

In this recipe, we will create a site (web) scoped feature that adds and configures our branding elements on the site.

How to do it...

Follow these steps to create a feature event receiver:

  1. Open the SiteBranding feature that was created when creating for our MasterPages and PageLayouts modules.
  2. Verify that both the modules are listed in the Items in the feature section.
    How to do it...
  3. Right-click on SiteBranding.feature in the Features folder.
  4. Select Add Event Receiver as shown in the following screenshot:
    How to do it...
  5. In our new SiteBrandingEventReceiver class, add the following constant strings that we will use for property names and master page URLs:
    public class SiteBrandingEventReceiver : SPFeatureReceiver
    {
    
    private const string PropertyOldMasterUrl = "CustomProp::OldMasterUrl";
    
    private const string PropertyOldCustomMasterUrl = "CustomProp::OldCustomMasterUrl";
    
    private const string TacomaMasterUrl = "_catalogs/masterpage/Tacoma.master";
    
    private const string SeattleMasterUrl = "_catalogs/masterpage/Seattle.master";	
  6. Uncomment the FeatureActivated and FeatureDeactivating methods as shown in the following code:
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
    }
    
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
    }
  7. In the FeatureActivated method, get the site in a using statement as follows:
    using (var web = properties.Feature.Parent as SPWeb)
  8. Ensure the site is not null using the following line of code:
    if (web != null)
  9. Get the current value for the AllowUnsafeUpdates property on the site and set the value to true as follows:
    var allowUnsafeUpdates = web.AllowUnsafeUpdates;
    
    web.AllowUnsafeUpdates = true;
    
    web.Update();
  10. Using the following code, remove our custom master page properties if they already exist on the site:
    if (web.AllProperties.ContainsKey(PropertyOldMasterUrl))
    web.AllProperties.Remove(PropertyOldMasterUrl);
    
    if (web.AllProperties.ContainsKey(PropertyOldCustomMasterUrl))
    web.AllProperties.Remove(PropertyOldCustomMasterUrl);
  11. Get the current master page settings for the site using the following code:
    var masterUrl = web.MasterUrl;
    
    var customMasterUrl = web.CustomMasterUrl;
  12. Set the current master page settings as the values to our custom master page properties.
    web.AllProperties.Add(PropertyOldMasterUrl, masterUrl);
    
    web.AllProperties.Add(PropertyOldCustomMasterUrl, customMasterUrl);
  13. Set the Tacoma.master master page as the master page for the site and system master pages using the following code:
    web.MasterUrl = TacomaMasterUrl;
    
    web.CustomMasterUrl = TacomaMasterUrl;
    
    web.Update();
  14. Set the AllowUnsafeUpdates property of the site back to its original value.
    web.AllowUnsafeUpdates = allowUnsafeUpdates;
    
    web.Update();
  15. In the FeatureDeactivating method, get the site in a using statement.
    using (var web = properties.Feature.Parent as SPWeb)
  16. Ensure the site is not null, using the following code:
    if (web != null)
  17. Get the current value for the AllowUnsafeUpdates property on the site and set the value to true.
    var allowUnsafeUpdates = web.AllowUnsafeUpdates;
    web.AllowUnsafeUpdates = true;
    web.Update();
  18. Get the default Seattle.master master page URL using the following code:
    var masterUrl = SeattleMasterUrl;
    
    var customMasterUrl = SeattleMasterUrl;
  19. Using the following code, check the site properties for the original master page settings we added in the FeatureActivating method:
    if (web.AllProperties.ContainsKey(PropertyOldMasterUrl))
    {
    
    var propertyValue = web.AllProperties[PropertyOldMasterUrl] as string;
    
    if (!string.IsNullOrEmpty(propertyValue))
    masterUrl = propertyValue;
    
    web.AllProperties.Remove(PropertyOldMasterUrl);
    
    }
    
    if (web.AllProperties.ContainsKey(PropertyOldCustomMasterUrl))
    {
    
    var propertyValue = web.AllProperties[PropertyOldCustomMasterUrl] as string;
    
    if (!string.IsNullOrEmpty(propertyValue))
    customMasterUrl = propertyValue;
    
    web.AllProperties.Remove(PropertyOldCustomMasterUrl);
    
    }
  20. Set the original master page as the master page for the site and system master pages.
    web.MasterUrl = masterUrl;
    
    web.CustomMasterUrl = customMasterUrl;
    
    web.Update();
  21. Set the AllowUnsafeUpdates property of the site back to its original value, using the following code:
    web.AllowUnsafeUpdates = allowUnsafeUpdates;
    
    web.Update();

How it works...

SharePoint features may include event receivers that execute at different points in the life cycle of the feature. In our example, we created FeatureActivated and FeatureDeactivating event receivers to configure the master page settings when activated or deactivated. In addition, we stored the old master page settings as properties on the site to allow us to restore the settings when the feature is deactivated.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.133.49