There's quite a list of tasks to complete if you want to move databases between web apps with different url’s. Knowing this, see if an Alternate Access Mapping (AAM) will work for you instead of moving databases between web apps.
Here's a list of things to be mindful: Note: this is not meant to be all inclusive.. just a list of things we thought were important.
<update: moved code to code snippet box>
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace MoveDBtoNewWebApp
{
class Program
static void Main(string[] args)
if (args.Length != 3) help();
else
//SPSite site = new SPSite(args[0].Trim());
string webappurl = args[0].Trim();
string portalName = args[1].Trim();
string portalurl = args[2].Trim();
try
//Bind to the top-level site
//TODO: will not work if no top level site exists but web application is present
//How do you bind to a web application directly?
SPSite toplevelsite = new SPSite(webappurl);
if (toplevelsite != null)
//Bind to web application that hosts top-level site
SPWebApplication spwebapp = toplevelsite.WebApplication;
//Close top-level site
toplevelsite.Dispose();
//Enumerate through all sites in web application
foreach (SPSite site in spwebapp.Sites)
//Set search center url for site collection
//GetWebProperties(site, portalurl, portalName);
SetWebProperties(site);
}
catch (Exception e)
Console.WriteLine(e.Message + " " + e.StackTrace);
finally
site.Dispose();
public static void SetWebProperties(SPSite site, string url, string name)
if (site != null)
site.PortalUrl = url;
site.PortalName = name;
Console.WriteLine("site: " + site.Url + " was configured successfully");
Console.WriteLine("error: " + site.Url + " " + e.Message);
//Ensure SPSite object is disposed.
//site is an orphan. Report to user.
Console.WriteLine("error " + site.Url + " is an orhpan.");
public static void help()
//Help not very verbose. This will be used once and possibly integrated with STSADM.
Console.WriteLine("");
Console.WriteLine("SPMapPortal2.exe is used to connect all sites under a web application to the specified portal");
Console.WriteLine("Usage");
Console.WriteLine("SPMapPortal2.exe <web app url> <portal description> <relative url to portal>");
Console.WriteLine("Example");
Console.WriteLine("SPMapPortal2.exe http://contoso.com \"portal description\" / ");