3

I've created a web template and as part of the provisioning process I set the SPWeb.CustomMasterUrl. I do this in code. When I create a site collection using this template it works fine but if I try to create a sub web using the template it errors out. Here's the code I use. The path is incorrect but just wondering what's the best approach to handle this situation?

thisWeb.CustomMasterUrl = thisWeb.ServerRelativeUrl + "/_catalogs/masterpage/GPMOMaster.master";

Whats the best way to deal with this scenario in a web template?

Ok i fixed this by using the code

//set master page
            if (thisWeb.IsRootWeb)
            {
                thisWeb.CustomMasterUrl = thisWeb.ServerRelativeUrl + 
                    "/_catalogs/masterpage/GPMOMaster.master";
            }
            else
            {
                thisWeb.CustomMasterUrl = thisWeb.Site.RootWeb.ServerRelativeUrl + 
                    "/_catalogs/masterpage/GPMOMaster.master";
            }
1

It's better to separate branding from the web template. Use the code RJ suggested to create an event receiver for your Branding Site-scoped feature.

Additionally, you can use the WebProvisioned method to set the master page when the sub-site gets created. It will automatically set the masterpage from your root's. Here's the sample code you could use.

Create a NewWebEventReceiver and add it your your Site-scoped branding feature.

public class NewWebEventReceiver : SPWebEventReceiver
{
    /// <summary>
    /// A site was provisioned.
    /// </summary>
    public override void WebProvisioned(SPWebEventProperties properties)
    {
        SPWeb currWeb = properties.Web;
        SPWeb rootWeb = currWeb.Site.RootWeb;

        //For SP 2010 remove the 'currWeb.IsAppWeb' check
        if (!currWeb.IsAppWeb)
        {
            currWeb.MasterUrl = rootWeb.MasterUrl;
            currWeb.CustomMasterUrl = rootWeb.CustomMasterUrl;
            // To set AlternateCssUrl and SiteLogoUrl
            // currWeb.AlternateCssUrl = rootWeb.AlternateCssUrl;
            // currWeb.SiteLogoUrl = rootWeb.SiteLogoUrl;
            currWeb.Update();
        }
    }
}
0

If you look at the path that gets generated the problem is pretty simple. Is your master page being deployed to the master page gallery of every subsite upon creation? Typically I just deploy the master page to the gallery at the root web of the site collection and use the same file for every subsite instead of having many copies of the same master page in every subweb's master page gallery.

Here's some code from a feature receiver. Try something like this:

SPSite site = (SPSite)(properties.Feature.Parent);
string masterUrlPath = site.ServerRelativeUrl;
if (masterUrlPath == "/")
    masterUrlPath = "/_catalogs/masterpage/GPMOMaster.master";
else
    masterUrlPath += "/_catalogs/masterpage/GPMOMaster.master";

web.CustomMasterUrl = masterUrlPath;
  • No the master page is deployed only at the site collection level. If my template is used as a subweb then i want it to reference the custom master page at the site collection level. thanks for the code. Im gonna give a go using a rootweb check – Richard Banks Jan 10 '14 at 15:35

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.