1

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();
            }
        }
    }
}
}
  • 1
    Look up on 'Active Deployment Configuration', that will explain why your feature is getting activated when deployed from VS. To automate the process, you'd need powershell for feature activation, better yet install the wsp via powershell too. – Aveenav Aug 25 '14 at 14:43
  • You have switched between getting activated and doesn't in your question three times now, which one is it? ;) – Robert Lindgren Aug 25 '14 at 15:46
  • lol. Yes it's monday morning and i didn't have enough coffee before i wrote it. To be clear,..i'm installing the wsp via powershell. The feature gets installed and activated but doesn't set the masterpage until i go into 'site collection features' and turn the feature off and back on again. – C. Poulain Aug 25 '14 at 16:10
0

First, if this is your real production code, you should really consider adding error handling with at least a try/catch blog and log the error to the SharePoint ULS log.

Second, your code fails because it assumes that the feature activation happens while on a particular SharePoint web site:

SPSite site = properties.Feature.Parent as SPSite;

If activating via PowerShell you will not get a valid SPSite object via the code above, while it works just fine if running via a Web browser UI on the site collection feature page.

Disabling the auto-activation and run the corresponding PowerShell code to activate the feature once the WSP has been installed and deployed should fix your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.