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";

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.

3 Answers 3


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


Or in your case:


So drop the oWeb.Url

  • I believe djeeg is correct. I thought from your tweet that you were using relative URLs, but if you're giving it absolute that could be the snag.
    – webdes03
    Commented Jan 21, 2011 at 16:09
  • Yep, I think this has fixed it, but I'm getting a "File Not Found" error, which I thought was related to the masterpage, but I've got no other errors in ULS or Event Logs, so I'm guessing something in the masterpage is b0rked. Cheers guys.
    – James Love
    Commented Jan 21, 2011 at 16:16
  • Additionally, this only works on the top level site collection for web application (under the root / managed path). Other site cols under other managed paths (/sites/) are throwing the error.
    – James Love
    Commented Jan 21, 2011 at 16:20

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.

  • Issue is (now) though, that this works on a site collection which rests on the / (root) managed path, so the url server. The problem arises when a site collection is under a managed path, such as server/departments/site
    – James Love
    Commented Jan 21, 2011 at 18:15
  • In that case I think you need to use the ~site syntax.
    – SPDoctor
    Commented Jan 21, 2011 at 18:58
  • Sorry, I think I mean ~sitecollection.
    – SPDoctor
    Commented Jan 21, 2011 at 19:01
  • Does that work when used in SPWeb.CustomMasterUrl? (I'll answer my own question once my VM starts to play ball :P)
    – James Love
    Commented Jan 21, 2011 at 19:35
  • Nopegetting "Invalid URL: ~sitecollection/_catalogs/masterpage/MyMaster.master"
    – James Love
    Commented Jan 21, 2011 at 20:57

Your Answer

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

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