Question

Has anyone had the error Invalid URL when setting the masterpage programmatically?

Using this code in a feature receiver (the feature provisions the masterpage to the gallery, that works fine)

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPSite oSite = (SPSite)properties.Feature.Parent;

        using (SPWeb oWeb = oSite.OpenWeb())
        {
            oWeb.CustomMasterUrl = oWeb.Url + "/_catalogs/masterpage/GIS.CZEN.MIST.master";
            oWeb.AllProperties["__InheritsCustomMasterUrl"] = "True";
            oWeb.Update();
        }
    }

If I activate this feature on site http://<server>/<path>/<site>/ then I'll get the error:

Invalid URL: http://<server>/<path>/<site>/_catalogs/masterpage/MyMaster.master

This happens with Team Sites as well as Publishing Sites, in SharePoint Server 2007.

EDIT: All suggestions are valid, but the real fix for me was using oWeb.ServerRelativeUrl instead of oWeb.Url.

Was it helpful?

Solution

I think you are supposed to set CustomMasterPage and MasterPage to be web relative urls like:

/_layouts/custom.master

Or in your case:

/_catalogs/masterpage/GIS.CZEN.MIST.master

So drop the oWeb.Url

OTHER TIPS

djeeg´s answer is correct, it is actually the ServerRelativeUrl of the SPFile object that represents the masterpage that you are after.

There is nothing fundamentally wrong with the url: http://server/site/subsite/_catalogs/masterpage/MyMaster.master as long as that is where you put your master page, i.e. in the master page gallery for your site. If you use the "server relative" address you will get the master page gallery for your site collection. They are different galleries.

In publishing sites, when you set the master page through the UI, it always looks in the site collection master page gallery, so we get into the habit of thinking that is the only one. Of course in an uncustomized state the contents of all these wp galleries may point to the same files ghosted in the file system.

Looks like you put your custom master page in your site collection wp gallery, but then tried to find it in the site wp gallery when setting the custom master on your site. Hope that throws more light on the errors you are getting.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top