I am trying to install our custom masterpage (and supporting files) as a feature which would automatically turn on and set the default masterpage to our custom one.
When deploying in VS2012, it works fine. The feature is installed & activated at the site collection level and the default masterpage is successfully changed.
But when I publish the solution and install the WSP via Solution Management in Central Administration, it doesn't work. The feature gets installed and activated at the site collection level BUT it doesn't set the masterpage to our default.
If i then turn the feature off and back on, the default masterpage is correctly set to our custom masterpage.
Our requirement is to have everything done automatically so having to turn the feature off and back on manually is something we would like to avoid.
I'd appreciate any tips or help to get this working.
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using System.Linq;
namespace our_masterpage.Features.Activate.Custom.Master
{
/// <summary>
/// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
/// </summary>
/// <remarks>
/// The GUID attached to this class may be used during packaging and should not be modified.
/// </remarks>
[Guid("fca75088-7182-4727-853a-76e82fdfc7a1")]
public class ActivateCustomEventReceiver : SPFeatureReceiver
{/// Activating the feature
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
if (site != null)
{
SPWeb topLevelSite = site.RootWeb;
// Calculate relative path to site from Web Application root.
string webAppRelativePath = topLevelSite.ServerRelativeUrl;
if (!webAppRelativePath.EndsWith("/"))
{
webAppRelativePath += "/";
}
// Activate publishing infrastructure
site.Features.Add(new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa"), true);
// Enumerate through each site and apply branding.
foreach (SPWeb web in site.AllWebs)
{
// Activate the publishing feature for all webs.
web.Features.Add(new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb"), true);
web.MasterUrl = webAppRelativePath + "_catalogs/masterpage/our-custom-masterpage.master";
web.CustomMasterUrl = webAppRelativePath + "_catalogs/masterpage/our-custom-masterpage.master";
web.Update();
}
}
}
/// Deactivating the feature
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite siteCollection = properties.Feature.Parent as SPSite;
if (siteCollection != null)
{
SPWeb topLevelSite = siteCollection.RootWeb;
// Calculate relative path to site from Web Application root.
string webAppRelativePath = topLevelSite.ServerRelativeUrl;
if (!webAppRelativePath.EndsWith("/"))
{
webAppRelativePath += "/";
}
// Enumerate through each site and apply branding.
foreach (SPWeb site in siteCollection.AllWebs)
{
site.MasterUrl = webAppRelativePath + "_catalogs/masterpage/seattle.master";
site.CustomMasterUrl = webAppRelativePath + "_catalogs/masterpage/seattle.master";
site.SiteLogoUrl = string.Empty;
site.Update();
}
}
}
}
}