1

I receive a "401 unauthorized" on http://mysite/_layouts/15/... after changing the default master page seattle.master to a custom master page. I am using the following PowerShell script:

$site_url = "http://mysite/"

$site = Get-SPSite $site_url
foreach ($web in $site.AllWebs)
{
    $web.MasterUrl = $site.RootWeb.ServerRelativeUrl +  "_catalogs/masterpage/custom.master";
    $web.CustomMasterUrl = $site.RootWeb.ServerRelativeUrl + "_catalogs/masterpage/custom.master";
    $web.Update();
    Write-Host $web.Url $web.MasterUrl $web.CustomMasterUrl;
}

All subsites and other site collections still seem to work.

The problem seems to be the difference between: http://mysite (working) and http://mysite/ (extra slash: not working). All links are linking to http://mysite/ (with slash). How to fix this?

  • Could you add how you define the $site_url? – moe Jan 8 '16 at 8:28
  • @moe See updated question – Citizen SP Jan 8 '16 at 8:32
  • Everything seems to be just fine as long as you use the URL as "/_catalogs/masterpage/custom.master"; (note the / at the begin). On a side note, not sure how your environment is set up, but you might have to apply the masterpage change to whole web application as it's possible to build My Sites so that each person's site is actually a site collection of its own. Confirm your hierarchy on Central Administration. – moe Jan 8 '16 at 9:48
  • @moe I get this error with the slash: 'mysite/search /_catalogs/masterpage/custom.master /_catalogs/masterpage/custom.master' Exception setting "MasterUrl": ""//_catalogs/masterpage/custom.master" contains invalid character '/'." – Citizen SP Jan 8 '16 at 9:52
1

The problem is your usage of ServerRelativeUrl.

ServerRelativeUrl always starts with / and ends with the rest of the URL to the site. For instance, server-relative URL of http://server/sites/site01 is /sites/site01 (no ending /).

However, in the case of a root site collection (i.e. http://server), the server-relative URL is simply /: it starts and ends with the same /...

The easy solution is to trim ending /, or to use a method such as SPUrlUtility.CombineUrl that will take care of such problems:

$site_url = "http://mysite/"

$site = Get-SPSite $site_url
foreach ($web in $site.AllWebs)
{
    $web.MasterUrl = [Microsoft.SharePoint.Utilities.SPUrlUtility]::CombineUrl($site.RootWeb.ServerRelativeUrl, "_catalogs/masterpage/custom.master");
    $web.CustomMasterUrl = [Microsoft.SharePoint.Utilities.SPUrlUtility]::CombineUrl($site.RootWeb.ServerRelativeUrl, "_catalogs/masterpage/custom.master");
    $web.Update();
    Write-Host $web.Url $web.MasterUrl $web.CustomMasterUrl;
}
1

Assuming $site_url is defined correctly, add /'s to your MasterUrl and CustomMasterUrl as following:

$site = Get-SPSite $site_url
foreach ($web in $site.AllWebs)
{
    $web.MasterUrl = $site.RootWeb.ServerRelativeUrl +  "/_catalogs/masterpage/custom.master";
    $web.CustomMasterUrl = $site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/custom.master";
    $web.Update();
    Write-Host $web.Url $web.MasterUrl $web.CustomMasterUrl;
}

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.