Recently, my colleague (Ankul Mathur) and me (much of the work done by him!) created a custom solution to change the SharePoint master page automatically on day to day basis.
Requirement: Change the master page based on the day of the week. Example; Monday should have Yellow master page and similarly each day will have its own master page. In a nutshell, the client wants a different looking very day. I know this is usability failure but can’t help!The requirement seems bit weird as we could have done this using CSS alone, but the master page components differ from one master page to the other.
Ok enough of bantering, lets dive deep into process;
Step 1: Download Gary Lapointe’s Custom Stsadm Command Line tool WSP file for MOSS or WSS from http://stsadm.blogspot.com/2009/02/downloads.html .
Step 2: Follow the steps giving in the article to install and activate the wsp service on the server using Stsadm command line tool.
Step 3: On the server under bin folder in 12 hive run the Stsadm command for setmasterpage to check the Stsadm for setmasterpage is working correctly.
Step 4: Create a Project in VS 2005/2008 named it CustomSTSADM and added a reference for commandlineprocess after download a file from http://www.codeguru.com/csharp/csharp/cs_misc/userinterface/article.php/c8503 . Here is the code;
Project.cs
1: using System;2: using System.Collections.Generic;3: using System.Linq;4: using System.Text;5: using twComps;6:7: namespace CustomSTSADM8: {9: class Program10: {11: static void Main(string[] args)12: {13: DateTime dt = DateTime.Now;14: string masterPage = string.Empty;15:16: switch (dt.DayOfWeek.ToString().ToLower())17: {18: case "monday":19: masterPage = System.Configuration.ConfigurationSettings.AppSettings["masterPageMon"];20: break;21: case "tuesday":22: masterPage = System.Configuration.ConfigurationSettings.AppSettings["masterPageTue"];23: break;24: case "wednesday":25: masterPage = System.Configuration.ConfigurationSettings.AppSettings["masterPageWed"];26: break;27: case "thursday":28: masterPage = System.Configuration.ConfigurationSettings.AppSettings["masterPageThu"];29: break;30: default:31: masterPage = System.Configuration.ConfigurationSettings.AppSettings["masterPageDef"];32: break;33: }34:35: string masterPageLink = System.Configuration.ConfigurationSettings.AppSettings["masterPageLink"];36:37: CommandLineProcess cmd = new CommandLineProcess();38: cmd.Command = "stsadm";39: cmd.Arguments = string.Format(" -o gl-setmasterpage -url \"{0}\" -sitemaster \"{1}{2}.master\" -systemmaster \"{3}{4}.master\"",40: System.Configuration.ConfigurationSettings.AppSettings["portalLink"],41: masterPageLink, masterPage, masterPageLink, masterPage);42:43: bool bSuccess = cmd.Start();44: }45: }46: }App.Config
1: <?xml version="1.0" encoding="utf-8" ?>2: <configuration>3: <appSettings>4: <add key="masterPageMon" value="blue" />5: <add key="masterPageTue" value="green" />6: <add key="masterPageWed" value="yellow" />7: <add key="masterPageThu" value="grey" />8: <add key="masterPageDef" value="pink" />9:10: <add key="masterPageLink" value="/_catalogs/masterpage/"/>11: <add key="portalLink" value="http://server name" />12:13: </appSettings>14: </configuration>
Step 5: Build the Project. This will create a folder with the project name inside that there will be a Bin folderàRelease folder à CustomSTSADm.exe, CommandLineProcess.dll, and CustomSTSADM.exe.config file.
Step 6: Copy all the above 3 files to bin folder under 12 hive on the server.
Step7: Run the .exe file on Windows Server 2003 and the master page is automatically set to the site set in the .exe.config file. And for Windows server 2008 right click on the .exe file and click on “Run as Administrator” it will set the new master page.
Step 8: Schedule the .exe file in Windows Scheduler to be run on daily/weekly/monthly basis.
On Windows 2008 Server;
Go to Start –> Programs –> Accessories –> System Tools –> Task Scheduler and set the following setting as shown in the images below:
As always, if you happen to know a much better way to accomplish this, please feel free to share.

