Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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

share|improve this answer
    
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 Jan 21 '11 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 Jan 21 '11 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 Jan 21 '11 at 16:20
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
    
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 Jan 21 '11 at 18:15
    
In that case I think you need to use the ~site syntax. –  SPDoctor Jan 21 '11 at 18:58
    
Sorry, I think I mean ~sitecollection. –  SPDoctor Jan 21 '11 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 Jan 21 '11 at 19:35
    
Nopegetting "Invalid URL: ~sitecollection/_catalogs/masterpage/MyMaster.master" –  James Love Jan 21 '11 at 20:57
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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