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.
Follow these steps to create a feature event receiver:
MasterPages
and PageLayouts
modules.Features
folder.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";
FeatureActivated
and FeatureDeactivating
methods as shown in the following code:public override void FeatureActivated(SPFeatureReceiverProperties properties) { } public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { }
FeatureActivated
method, get the site in a using
statement as follows:using (var web = properties.Feature.Parent as SPWeb)
if (web != null)
AllowUnsafeUpdates
property on the site and set the value to true
as follows:var allowUnsafeUpdates = web.AllowUnsafeUpdates; web.AllowUnsafeUpdates = true; web.Update();
if (web.AllProperties.ContainsKey(PropertyOldMasterUrl)) web.AllProperties.Remove(PropertyOldMasterUrl); if (web.AllProperties.ContainsKey(PropertyOldCustomMasterUrl)) web.AllProperties.Remove(PropertyOldCustomMasterUrl);
var masterUrl = web.MasterUrl; var customMasterUrl = web.CustomMasterUrl;
web.AllProperties.Add(PropertyOldMasterUrl, masterUrl); web.AllProperties.Add(PropertyOldCustomMasterUrl, customMasterUrl);
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();
AllowUnsafeUpdates
property of the site back to its original value.web.AllowUnsafeUpdates = allowUnsafeUpdates; web.Update();
FeatureDeactivating
method, get the site in a using
statement.using (var web = properties.Feature.Parent as SPWeb)
null
, using the following code:if (web != null)
AllowUnsafeUpdates
property on the site and set the value to true
.var allowUnsafeUpdates = web.AllowUnsafeUpdates; web.AllowUnsafeUpdates = true; web.Update();
Seattle.master
master page URL using the following code:var masterUrl = SeattleMasterUrl; var customMasterUrl = SeattleMasterUrl;
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); }
web.MasterUrl = masterUrl; web.CustomMasterUrl = customMasterUrl; web.Update();
AllowUnsafeUpdates
property of the site back to its original value, using the following code:web.AllowUnsafeUpdates = allowUnsafeUpdates; web.Update();
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.
3.142.133.54