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.
Follow these steps to create a site collection feature and the event receivers:
Features
folder.SiteCollectionBranding
.$Resources:Code6587ENCh04,Feature_SiteCollectionBranding_Title;
and Description to $Resources:Code6587ENCh04,Feature_SiteCollectionBranding_Description;
.SiteCollectionBranding
feature.ApplySiteBranding
).ApplySiteBranding.cs
file.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");
WebProvisioned
method, get the site in a using
statement as follows:using (var web = properties.Web)
null
.if (web != null)
if (web.Features[BrandingFeatureId] == null) web.Features.Add(BrandingFeatureId);
ApplySiteBranding.cs
file.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");
FeatureActivated
method.using
statement as follows:using (var site = properties.Feature.Parent as SPSite)
null
.if (site != null)
foreach (SPWeb web in site.AllWebs)
null
and that it exists.if (web != null && web.Exists)
if (web.Features[BrandingFeatureId] == null) web.Features.Add(BrandingFeatureId);
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.
18.119.106.237