It has been a while since my last post and today I am going to show you how to code a custom ‘stsadm’ command to change the url of a given top navigation tab. the reason I had to write this command was that, my client is using backup/restore to promote the site collection to different environments. when this happens all the non-relative urls are carried over to new environment that are not relevant to the new environment. so, one option is to change the urls in the UI. but, unfortunately the site collection has more than 500 sub-sites under it and changing them manually is nearly impossible.
Now, let’s do some coding.
There will be 3 required parameters in the command.
Url of the targeted site, title of the top navigation tab that url needs to be changed and the last one is the new Url for the tab. and also we need to have few utility methods that iterates through the site collection/site and returns the list of SPWeb objects.
Now, I will define the usage string for the command as follows.private string usageString = "[-siteurl < site url>] - site collection url or site url\n" +
"\t[-targettitle <url>] - title of the navigation item that needs to be replaced with new url\n" +
"\t[-newurl <url>] - new url \n";
public string GetHelpMessage(string command)now, let’s get to the code where magic happens.
{
return usageString;
}
public int Run(string command, StringDictionary keyValues, out string output)
{
int rtnCode = 0;
string cmd = command.ToLowerInvariant();
switch (cmd)
{
case "spupdatenavigation":
rtnCode = this.Traverse(keyValues);
break;
default:
throw new InvalidOperationException();
}
output = "";
return rtnCode;
}
my strategy here is to navigate trough all the sites that were returned by previous methods and get the Global navigation Node Collection for each site. and , then for each node, recursively navigate to the targeted node, change and update the url.
Once, you have sign the project and compile the code, one more step is required to register the command in the system. to register the command in the system I create the a xml file ‘stsadmcommands.xxx.xml’ in 12\CONFIG and the file name should be in the format of ‘stsadmcommands.<custom name>.xml’. the file contains the following;
<?xml version="1.0" encoding="utf-8" ?>After copying the dll file to GAC recycle the AppPool and you are all set to go.
<commands>
<command name="spupdatenavigation" class="XXX.Utils.Commands.SPUpdateNavigationCommand, XXX.Utils.Commands, Version=1.0.0.0, Culture=neutral, PublicKeyToken='public key token'"/>
</commands>
This is how you will use the command at the command line;
stsadm -o spupdatenavigation -siteurl http://server01:9090 -newurl http://server05:8888/recordscenter -targettitle "Records Center"
optionally, if you want to debug the code , you can provide additional key ‘-debug’ at the command.
The whole project can be found in this link.