I’d thought about building an STSADM command to enable setting the master page of a site for quite a while but had opted not to do it simply out of principle – it’s generally a better idea to do this via a Feature and I didn’t really want to promote a bad practice. Ultimately though I had to concede that there are administrators who will not have the luxury of having developers who can create Feature that can be deployed to enable consistent application of their master page across site collections.
So what I came up with was a command, called gl-setmasterpage, which allows the user to set the site and system master page URLs and, this is the cool part, copy a master page from a source location to the destination site. So consider that you have 10 different site collections on your main portal and you want all those site collections to use the same master page as the root site collection – you could accomplish this by running the following for each site collection (or by wrapping in a loop using PowerShell as shown further down):
stsadm -o gl-setmasterpage -url "http://portal/division1" -sitemaster "/division1/_catalogs/masterpage/custom.master" -systemmaster "/division1/_catalogs/masterpage/custom.master" –sitesource "http://portal/_catalogs/masterpage/custom.master"
The code to set the master page is pretty simple – there’s two core properties of the SPWeb object: CustomMasterUrl and MasterUrl. The CustomMasterUrl property corresponds to the “Site Master Page” value when editing the settings via the browser and the MasterUrl property corresponds to the “System Master Page” value. The bulk of the code for this command is in the validation and copying of the source master page:
The help for the command is shown below:
C:\>stsadm -help gl-setmasterpage stsadm -o gl-setmasterpage Sets the site and/or system master page for the given web. Parameters: -url <web URL> [-sitemaster <server relative URL to the site master page>] [-systemmaster <server relative URL to the system master page>] [-resetsubsites] [-systemsource <URL to a source system master page file to copy to the target> [-sitesource <URL to a source site master page file to copy to the target>
The following table summarizes the command and its various parameters:
|Command Name||Availability||Build Date|
|gl-setmasterpage||WSS 3.0, MOSS 2007||Released: 2/7/2009
|Parameter Name||Short Form||Required||Description||Example Usage|
|url||Yes||URL of the web or site collection to update.||-url "http://portal"|
|sitemaster||sitemp||Yes if systemmaster is not provided or sitesource is provided||The server relative URL to the master page.||-sitemaster "/_catalogs/masterpage/default.master"
|systemmaster||sysmp||Yes if sitemaster is not provided or systemsource is provided||The server relative URL to the master page.||-systemmaster "/_catalogs/masterpage/default.master"
|resetsubsites||reset||No||If specified all sub-sites of the passed in URL will be configured to use the specified master page.||-resetsubsites
|systemsource||syssrc||No||The absolute URL to the source master page to copy to the master page gallery of the web specified in the URL parameter.||-systemsource "http://portal/_catalogs/masterpage/default.master"
|sitesource||sitesrc||No||The absolute URL to the source master page to copy to the master page gallery of the web specified in the URL parameter.||-sitesource "http://portal/_catalogs/masterpage/default.master"
Now that we have the command we can easily combine this with a simple bit of PowerShell that will enable us to copy a master page from a single source site to all sites that match our filter criteria and set the master page settings to use this new master page. The code to do this is shown in lines 1-6 below along with some sample output in the following lines: