As part of the upgrade I was working on we wanted to move site collections from the upgraded location to a new location on a different web application. This can be done really easily using the backup and restore built-in commands in conjunction with the deletesite command. But I wanted to make this work in one step with the upgrade so I decided to extend the upgrade2 command to enable the moving of sites. I figured though, if I’m going to write the code to do this why not abstract the ability out into a separate command all it’s own. The result is a new command called gl-movesite. The nice thing about this is that it ends up doing what you would think the new renamesite command (part of SP1) would do – if you’ve tried to use this most likely you’ve noticed that it only works with host header mapped sites so the majority of us that aren’t using host header mapped sites the command is pretty useless. The code to do this is really simple – in fact I’m not doing anything unique – I’m just calling out to the built-in stsadm commands – I suppose I could have written the code to use the API directly but this was easier and accomplished the same thing and I was able to put it together in about 10 minutes.

 1internal static void MoveSiteCollection(string existingUrl, string newUrl)
 2{
 3    /*stsadm.exe -o backup
 4    -url <url>
 5    -filename <filename>
 6    [-overwrite]*/
 7    string filename = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".bak");
 8
 9    string cmd = string.Format(" -o backup -url \"{0}\" -filename \"{1}\"", existingUrl, filename);
10
11    if (Utilities.RunStsAdmOperation(cmd.ToString(), true) != 0)
12        throw new SPException("Error occured backing up site for move.\r\nCOMMAND: stsadm.exe" + cmd);
13
14    /*stsadm.exe -o deletesite
15    -url <url>
16    -deleteadaccounts <true/false>*/
17    cmd = string.Format(" -o deletesite -url \"{0}\"", existingUrl);
18
19    if (Utilities.RunStsAdmOperation(cmd.ToString(), true) != 0)
20        throw new SPException("Error occured deleting source site for move.\r\nCOMMAND: stsadm.exe" + cmd);
21
22    /*stsadm.exe -o restore
23    -url <url>
24    -filename <filename>
25    [-hostheaderwebapplicationurl <web application url>]
26    [-overwrite]*/
27
28    cmd = string.Format(" -o restore -url \"{0}\" -filename \"{1}\"", newUrl, filename);
29
30    try
31    {
32        if (Utilities.RunStsAdmOperation(cmd.ToString(), true) != 0)
33        throw new SPException("Error occured restoring site for move.\r\nCOMMAND: stsadm.exe" + cmd);
34    }
35    catch (SPException)
36    {
37        Console.WriteLine("\r\nSite backup file can be located here: {0}", filename);
38        throw;
39    }
40    File.Delete(filename);
41}

The syntax of the command can be seen below:

C:\>stsadm -help gl-movesite

stsadm -o gl-movesite

Moves a site collection (wraps the backup, deletesite, and restore operations into one operation)

Parameters:
        -url <url of site collection to move>
        -moveto <url of new target path>

Here’s an example of how to move a site collection:

stsadm -o gl-movesite -url "http://intranet/sites/teamsite1" -moveto "http://teamsites/collaboration/teamsite1"

Note that the resultant name of the site collection can change.