Change the URL of a Site Collection in SharePoint 2010

Tags: Administration, PowerShell, SharePoint 2010

The “Discovery” phase of a project is always an interesting one.  Of course, sometimes what we discover needs to adapt to the business and how it’s changing.  In this post, I’ll walk you through a real-world scenario that I’ve gone through in the past couple of days.

Scenario

Imagine you work on a project with some business analysts (BAs) for a customer and the BAs determine there’s a need for a “section” ABC of the customer’s intranet.  Prior to determining this, you’ve already done all of your planning as far as web applications, site collections, and content databases are concerned.  So, being the amazing administrator that you are, you create a site collection for this “section” as follows:

Parent Web Application:  http://intranet.company.com
Site Collection Name: SectionABC
Site Collection Address: 
http://intranet.company.com/sites/SectionABC
Site Collection Content Database:  SP2010_Content_Sections

Well done!  Your team has discovered the customer’s need for a site collection and you’ve provisioned it according to your information architecture and governance.  Aren’t you a diligent little admin?

Meanwhile, back at the ranch…the BAs learn that what they thought was section ABC is really section XYZ!  Now what?  You know in your heart of hearts that it’s ideal for not only the name of the site collection to change, but also the URL.  You discuss this with your team and explain the reasoning behind it and everyone agrees that the URL should change from http://intranet.company.com/sites/SectionABC to http://intranet.company.com/sites/SectionXYZ.  Now it’s up to you to make that happen.

Supplemental Information

One of the additional things that you’ve done while the SharePoint architecture is being built out is ensure that site collections are backed up daily.  This is only temporary while things are changing so rapidly.  In the long run, site collection backups won’t necessarily need to be taken daily, but you’re doing that for now using the following PowerShell script.  For your convenience, you can download this script from my SkyDrive herehttp://sdrv.ms/TPJJH6.  Of course, I have to instruct you to use this at your own risk.  It works for me, but I can’t guarantee it will behave in your environment.

   1:  Write-Host "Starting Backup of All Site Collections"

   2:  Add-PSSnapIn Microsoft.SharePoint.PowerShell

   3:   

   4:  New-EventLog -LogName "Application" -Source "SharePoint Site Collection

Backup Script" -ErrorAction SilentlyContinue | Out-Null

   5:  $todaysDate = get-date -Format yyyy-MM-dd

   6:  $backupPath = "\\BackupPath\$todaysDate"    #Change "BackupPath"

accordingly

   7:   

   8:  try

   9:  {

  10:    $newFolder = new-item $backupPath -type directory

  11:    $webApps = Get-SPWebApplication  #Get all web applications (excluding
Central Administration)

  12:    $siteCollections = $webApps | ForEach-Object {$_.Sites}  #Get all site

collections

  13:    $siteCollections | ForEach-Object   {

  14:    $trimmedUrl = $_.Url.SubString(7,$_.Url.Length-7)

  15:    $fileName = $trimmedUrl.Replace('/','.')

  16:    Backup-SPSite -Identity $_.Id -Path $backupPath\$fileName.bak

  17:    $s = "$([DateTime]::Now.ToString('yyyy.dd.MM HH:mm:ss')) : SUCCESS :
""$($trimmedUrl)"""

  18:    $logFile=((Split-Path ($MyInvocation.MyCommand.Path))+"\lastrunlog.txt")

  19:    if( Test-Path $logFile -PathType Leaf )

  20:    {

  21:      $c = Get-Content $logFile

  22:      $cl = $c -split '`n'

  23:      $s = ((@($s) + $cl) | select -First 200)

  24:    }

  25:    Out-File -InputObject ($s -join "`r`n") -FilePath $logFile}

  26:  }

  27:  catch

  28:  {

  29:    Write-EventLog -Source "SharePoint Site Collection Backup Script" 
-Category 0 -ComputerName "." -EntryType Error -LogName "Application" –Message
"SharePoint site collection backup failed." -EventId 54321

  30:    $s = "$([DateTime]::Now.ToString('yyyy.dd.MM HH:mm:ss')) : FAILURE :
$($_.Exception.Message)"   

  31:    $filename=((Split-Path ($MyInvocation.MyCommand.Path))+"\lastrunlog.txt")

  32:    if( Test-Path $filename -PathType Leaf )

  33:    {

  34:      $c = Get-Content $filename

  35:      $cl = $c -split '`n'

  36:      $s = ((@($s) + $cl) | select -First 200)

  37:    }

  38:    Out-File -InputObject ($s -join "`r`n") -FilePath $filename

  39:  }


Great!  You’ve got daily site collection backups and next you’ll see how handy this can be.  In particular, this script has backed up the “SectionABC” site collection as intranet.company.com.sites.sectionabc.bak.  Now it’s time to change that pesky URL from http://intranet.company.com/sites/SectionABC to http://intranet.company.com/sites/SectionXYZ.

Solution

To do this, you’ll go through the following steps:

    1. If necessary (and it may not be depending on how far along you are), send out appropriate communications to the involved parties regarding this change, why it’s happening, and when it’s happening.

    1. Create a new Content Database in Central Administration.

      1. Central Administration > Application Management > Manage content databases (under Databases)
        image

      1. Select the appropriate web application.  In this scenario I’d select http://intranet.company.com.

      1. Click “Add a content database” just above the list of existing databases.

      1. Double-check the web application to ensure you’ve selected the correct one.

      1. Input the name of the database server.

      1. Input the name of the new database that you’d like to create.  Let’s pretend I’ve called it SP2010_Content_OldSections.

      1. Change any other settings as you see fit, but the default would work as well.

      1. Click OK to provision the database.

    1. If you haven’t already, become familiar with PowerShell in the SharePoint 2010 Management Shellhttp://msdn.microsoft.com/en-us/library/ee536539(v=office.14).aspx.

      Please note that if you skip step 4 below, then step 5 will fail because you cannot have two site collections in the same content database that share the same GUIDs.

    1. Use the PowerShell cmdlet Move-SPSite to move the SectionABC site collection to the new Content Database.  Details on how to use this cmdlet can be found herehttp://technet.microsoft.com/en-us/library/ff607915(v=office.14).aspx.  As an alternative, you can fire up the SharePoint 2010 Management Shell (run as Administrator if you have issues running it “normally”) and enter Get-Help Move-SPSite -Examples

      1. In this scenario, I would run:  Move-SPSite http://intranet.company.com/sites/SectionABC -DestinationDatabase SP2010_Content_OldSections.

      1. The site collection has now been moved to the appropriate database.  Next, you need to restore the site collection from its backup (intranet.company.com.sites.sectionabc.bak) to the correct URL in the correct database.

    1. Use the PowerShell cmdlet Restore-SPSite to restore the SectionABC site collection to the SectionXYZ URL and the correct content database.  Details on how to use this cmdlet can be found herehttp://technet.microsoft.com/en-us/library/ff607788(v=office.14).aspx.  As an alternative, you can fire up the SharePoint 2010 Management Shell (run as Administrator if you have issues running it “normally”) and enter Get-Help Restore-SPSite -Examples.

      1. In this scenario, I would run:  Restore-SPSite http://intranet.company.com/sites/SectionXYZ -Path “\\BackupPath\intranet.company.com.sites.sectionabc.bak” -DatabaseServer NameOfDatabaseServer –DatabaseName SP2010_Content_Sections.

      1. The site collection has now been restored to the correct URL preserving all configuration and security settings as well as content.

    1. Change the name of the restored site collection to “SectionXYZ.”  As I just mentioned, none of the content changed.  The only thing that has changed is the URL and that means that the actual “Title” of the top-level site in the site collection is still “SectionABC.”

      1. Site Actions > Site Settings

      1. In the “Look and Feel” section, click “Title, description, and icon.”
        image

      1. Change the value in the “Title” field from “SectionABC” to “SectionXYZ” and click OK.

    1. OPTIONAL:  Delete the old site collection and associate content database.  This would be the original site collection at http://intranet.company.com/sites/SectionABC (which should still be accessible) and its containing database named SP2010_Content_OldSections.  You certainly don’t need to delete these items, but it’s a good idea to do so in an effort to keep your environment cleaned up.  Alternatively, you might want to retain them for some time just in case and if you do, I recommend removing everyone’s permissions from it aside from your own.  You could even set the database as read-only if you like.  This is all up to you to figure out.

    1. Remember way back up in step 1 where you sent out communications to the involved parties?  Don’t forget to let them know that you’re done!  If necessary (and it may not be depending on how far along you are), send out appropriate communications to the involved parties regarding the completion of this change.  It’s also a good idea to include the new URL and advise folks to update their favorites/bookmarks.

Done!  After going through all of these steps you should have successfully changed the URL of a site collection. 

What’s that you say?  All this for information architecture?  Who cares?  Is it worth the effort?  These are all valid questions, and in my own opinion, yes it’s worth it for my own peace of mind if nothing else.  It should make my administrative life easier in the long run, too.  When it comes to navigation and search, having the correct URL in place is very beneficial.