STSADM Command Extensions for SharePoint (ApplyMaster Example)
My colleague Mauro Cardarelli posted a simple code snippet yesterday to determine the GUID for an SSP. It worked, and he accomplished what he wanted to accomplish, but my first thought was: STSADM command extension.
If you’ve worked with SharePoint much at all on the server, you’re very familiar with STSADM. It’s the command line tool that lets you interact with SharePoint. The Technet reference for STSADM commands shows you what is available “out of the box”. (The MOSS poster is here, and the WSS poster is here.)
Mauro’s task is the sort of thing that is an excellent candidate for an STSADM extension. This would make it a part of your toolkit and available the next time you need it.
If you want to understand the basics behind creating STSADM command extensions, check out this Microsoft article: http://msdn.microsoft.com/en-us/library/bb862109.aspx. If you want to see what you really can do, though, Gary Lapointe is the master: http://stsadm.blogspot.com/2007/08/stsadm-commands_09.html
I used Gary’s gl-applytheme command as the basis for another command that I needed to create to deploy a new master page to 250+ Site Collections. The core code is listed below. To apply my new master page across all of the Site Collections, I simply deployed my custom applymaster command to the server and then ran a batch file which contained a line for each Site Collection.
/// <summary> /// Applies the master to web. /// </summary> /// <param name="master">The master.</param> /// <param name="url">The URL.</param> /// <param name="recurse">if set to <c>true</c> [recurse].</param> internal static void ApplyMasterToWeb(string master, string url, bool recurse) { using (SPSite site = new SPSite(url)) using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(url)]) { ApplyMasterToWeb(master, web, recurse); } } /// <summary> /// Applies the master to web. /// </summary> /// <param name="master">The master.</param> /// <param name="web">The web.</param> /// <param name="recurse">if set to <c>true</c> [recurse].</param> internal static void ApplyMasterToWeb(string master, SPWeb web, bool recurse) { web.MasterUrl = master; web.CustomMasterUrl = master; web.Update(); if (recurse) { foreach (SPWeb subWeb in web.Webs) { try { ApplyMasterToWeb(master, subWeb, recurse); } finally { subWeb.Dispose(); } } } }