I've moved my blog to!. Please update your links. This blog is no longer in use--you can find all posts and comments at my new blog; I will no longer be posting to this site and comments have been disabled.

Tuesday, January 22, 2008

Move a Site Collection

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");
   9:  string cmd = string.Format(" -o backup -url \"{0}\" -filename \"{1}\"", existingUrl, filename);
  11:  if (Utilities.RunStsAdmOperation(cmd.ToString(), true) != 0)
  12:   throw new SPException("Error occured backing up site for move.\r\nCOMMAND: stsadm.exe" + cmd);
  14:  /*stsadm.exe -o deletesite
  15:     -url <url>
  16:     -deleteadaccounts <true/false>*/
  17:  cmd = string.Format(" -o deletesite -url \"{0}\"", existingUrl);
  19:  if (Utilities.RunStsAdmOperation(cmd.ToString(), true) != 0)
  20:   throw new SPException("Error occured deleting source site for move.\r\nCOMMAND: stsadm.exe" + cmd);
  22:  /*stsadm.exe -o restore
  23:   -url <url>
  24:   -filename <filename>
  25:   [-hostheaderwebapplicationurl <web application url>]
  26:   [-overwrite]*/
  28:  cmd = string.Format(" -o restore -url \"{0}\" -filename \"{1}\"", newUrl, filename);
  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)

        -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.


Mat said...


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

Gary Lapointe said...

Yes - check out my "gl-moveweb" command.

smitty said...


I need to convert a path-based site collection into a host-named site collection. Do you think that gl-movesite will work for that?


Gary Lapointe said...

Russ - I haven't tried that but it stands to reason that it should. I'm really just wrapping the backup/restore and deletesite commands - nothing special.

Heiko Hatzfeld said...

What is the best way to convert a site Collection to a site?

Gary Lapointe said...

Try my gl-moveweb command - it's worked well for me in the past.

Anonymous said...


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.


Gary Lapointe said...

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).

Lester said...

It would be good if this command could allow specifying the content database for the moved site collection as with Createsiteinnewdb.

Kanta said...

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

Gary Lapointe said...

Kanta - you should be able to use my gl-exportlist and gl-importlist to move libraries between your farms.

WebApe said...

Gary, have you run into size limits for this process? We have a 22Gb site collection which is more that what is recommended for the backup and restore stsadm commands but not by much.I have tested this on a smaller collection, however, I am a little nervous about the larger one.