0

I've created and deployed a custom master page and kept its scope to be Web because I want to apply to specific sites not the complete site collection. But when I activate feature at a specific site, it automatically gets activated to the parent site and thus each site. This happens only the first time a feature is activated after deploying. There after( After I've manually deactivated that feature from the parent site) it works fine. Please can anyone suggest something so that it gets activated on the specific site only? It's been chewing off my mind for quite sometime now :/

Following is the code I've used on Feature activated method of feature event receiver

      public override void FeatureActivated(SPFeatureReceiverProperties properties)
      {
        using (var web = (SPWeb)properties.Feature.Parent)
        {
            string customUrltoUse = web.ServerRelativeUrl + CustomizedMasterUrl;
            customUrltoUse = customUrltoUse.Replace("//", "/");

            // Store the old Master URL's and Custom Master URL's
            web.AllProperties["OldMasterUrl"] = web.MasterUrl;
            web.AllProperties["OldCustomMasterUrl"] = web.CustomMasterUrl;
            web.AllProperties["__InheritsCustomMasterUrl"] = "False";
            web.AllProperties["__InheritsMasterUrl"] = "False";
            // Assign the Master URL to both properties

            try
            {
                if (web.IsRootWeb)
                {
                    foreach (SPWeb subweb in web.Webs)
                    {
                        if (subweb.WebTemplateId != 50 && subweb.WebTemplateId != 14483)
                        {
                            subweb.MasterUrl = customUrltoUse;
                            subweb.CustomMasterUrl = customUrltoUse;
                            subweb.Update();

                            if (subweb.Webs.Count > 0)
                            {
                                foreach (SPWeb subsubweb in subweb.Webs)
                                {
                                    subsubweb.MasterUrl = customUrltoUse;
                                    subsubweb.CustomMasterUrl = customUrltoUse;
                                    subsubweb.Update();
                                }
                            }
                        }
                    }
                }

                //SPFile uipmasterPage = web.GetFile(web.ServerRelativeUrl + "/_catalogs/masterpage/uipcustom.master");

                //uipmasterPage.Delete();
            }
            catch
            {
            }

            web.MasterUrl = customUrltoUse;
            web.CustomMasterUrl = customUrltoUse;

            web.Update();   }
    }

1 Answer 1

1

When deploying Masterpages and Pagelayouts, the feature deploying them should always be SITE scoped as they can only be added to site collection root levels.

Then if you want to control which subsites should use the deployed masterpage (if you do not have Publishing Features activated), you create a new feature, which is WEB scoped, with a FeatureReceiver containing code to set the masterpage for the current web.

4
  • 1
    @ankitaalung Because Visual Studio don't have a chance of knowing what you're gonna use the feature for. The developer needs to know what can be done and what cannot be done at certain levels of SharePoint sites. Site scope is for Site Collection, and Web scope is for sites in the site collection.
    – user2536
    Jul 31, 2013 at 11:27
  • Thanks, but I tried it using your method and the same is happening in same scenario. I commented the code for methods in first feature(Scope:Site) and used the code of event receiver of second feature(Scope:web). When I deploy it, master page is automatically applied to the site collection which was my headache from beginning. Jul 31, 2013 at 12:18
  • If you use the code that you have posted in your question, the code in the try clause is the issue as you loop through every single web if the web variable is web.IsRootWeb
    – user2536
    Jul 31, 2013 at 12:29
  • What do i do about it? I don't want it to be applied by default on site collection the moment it is deployed. :/ Jul 31, 2013 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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