Move a Site Collection

Posted on Posted in SharePoint 2007, STSADM Commands

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.

   1: internal 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.

15 thoughts on “Move a Site Collection

  1. Hi,

    The method you mention here is to do with moving a Site Collection. Is there a similar method for moving just a site/subsite.

  2. Hi,

    looks like very helpful tool. but i wanted to know how can i use them? i wanted to move my sitecollection to another url. i think its the movesite command.

    but i want to know how can i use that command?

    i mean do i need to build the project and then add the dll in the gac or what?

    please guide as i am a new commer in this field.

    thanks

  3. Sorry for not responding earlier – your comment got lost in my emails while I was traveling. There are install instructions on the download page – you basically just use the built in stsadm commands to install. If you want to move a site collection to a new url (within a farm) then you should use the built in backup and restore commands. If you’re going to a new farm then you should use the built in export and import commands (or my gl-import2 which provides additional options over the built-in import command).

  4. Looks very useful tool. well I need to know what will be best option to How to move/migrate SharePoint list items/documents and preserve metadata properties at the same time from WSSv3 to SharPoint 2007

  5. Hi Gary – Can I use your gl-movesite tool to move a Site Collection into another Site Collection in the same Web Application? Does MOSS even support that? Thanks!

    1. It’s been a while since I’ve done it so I can’t remember if it’s the gl-movesite or gl-moveweb but one of them will do it for you. It’s a bit more complex if the site is a publishing site but it is doable (though there are tons of caveats around it).

  6. I am converting a path based site collection to a host name site collection. Will this work for that. I am still getting my sea legs in SharePoint, so, sorry if this is an obvious question.

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA

*