Creating the site collection feature to apply the feature to new and existing sites

In this recipe, we will create a site collection scoped feature that will activate the site scoped branding feature to all sites in the site collection. In addition, we will add an event receiver to activate the site branding feature on all newly created sites.

How to do it...

Follow these steps to create a site collection feature and the event receivers:

  1. Right-click on the Features folder.
  2. Click on Add Feature as shown in the following screenshot:
    How to do it...
  3. Rename the new feature SiteCollectionBranding.
  4. Open the new SiteCollectionBranding feature.
  5. Set Scope to Site.
  6. Set Title to $Resources:Code6587ENCh04,Feature_SiteCollectionBranding_Title; and Description to $Resources:Code6587ENCh04,Feature_SiteCollectionBranding_Description;.
  7. Save the SiteCollectionBranding feature.
    How to do it...
  8. Right-click on the project name.
  9. Click on Add and then select New Item.
  10. Navigate to Visual C# Items | Office/SharePoint and select Event Receiver, as shown in the following screenshot:
    How to do it...
  11. Give the event receiver a name (for instance, ApplySiteBranding).
  12. Click on Add.
  13. Select Web Events for What type of event receiver do you want? as shown in the following screenshot:
    How to do it...
  14. Select A site was provisioned under Handle the following events as shown in the following screenshot:
    How to do it...
  15. Click on Finish.
  16. Open the SiteBranding feature.
  17. Ensure that the ApplySiteBranding event receiver is not listed under Items in the feature.
  18. In the Properties pane, make a note of the Feature Id. We will use this later.
    How to do it...
  19. Open the SiteCollectionBranding feature.
  20. Ensure the ApplySiteBranding event receiver is listed under Items in the feature as shown in the following screenshot:
    How to do it...
  21. In the Properties pane, make a note of the feature ID. We will use this later.
  22. In the ApplySiteBranding event receiver, open the ApplySiteBranding.cs file.
  23. In the ApplySiteBranding class, add a static GUID for our SiteBranding feature ID. Replace the sample feature ID with the feature ID from your SiteBranding feature.
    public class ApplySiteBranding : SPWebEventReceiver
    {
    
    private static Guid BrandingFeatureId = new Guid("1150dec7-4af6-44d8-b241-d976d26b723c");
  24. In the WebProvisioned method, get the site in a using statement as follows:
    using (var web = properties.Web)
  25. Ensure the site is not null.
    if (web != null)
  26. Verify the feature is in the collection of features activated on the site. If it is not activated, add the feature to the collection as follows:
    if (web.Features[BrandingFeatureId] == null)
    web.Features.Add(BrandingFeatureId);
  27. Save the ApplySiteBranding.cs file.
  28. Add an event receiver to the SiteCollectionBranding feature.
  29. In the SiteCollectionBrandingEventReceiver class, add a static GUID for our SiteBranding feature ID. Replace the sample feature ID with the feature ID from your SiteBranding feature.
    public class SiteCollectionBrandingEventReceiver : SPFeatureReceiver
    {
    private static Guid BrandingFeatureId = new Guid("1150dec7-4af6-44d8-b241-d976d26b723c");
  30. Uncomment the FeatureActivated method.
  31. Get the site collection in a using statement as follows:
    using (var site = properties.Feature.Parent as SPSite)
  32. Ensure the site collection is not null.
    if (site != null)
  33. Iterate through each site in the site collection.
    foreach (SPWeb web in site.AllWebs)
  34. Ensure the site is not null and that it exists.
    if (web != null && web.Exists)
  35. Verify the feature is in the collection of features activated on the site. If it is not activated, add the feature to the collection using the following code:
    if (web.Features[BrandingFeatureId] == null)
    web.Features.Add(BrandingFeatureId);
  36. Save the event receiver.

How it works...

In this recipe, we first created a new feature definition for our site collection feature. We then added an event receiver that is triggered any time a new site is created in the site collection. In this event receiver, we are ensuring the site branding feature is activated on newly created sites.

Next, we added an event receiver that is triggered when our new site collection feature is activated. In this event receiver, we are iterating through each site in the site collection to ensure the site branding feature is activated on all existing sites.

The most unique identifiers for SharePoint elements created in Visual Studio, such as the Feature Id, will be automatically generated when the item is created.

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.119.106.237