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)
{
try
{
//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();
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message + " " + e.StackTrace);
}
finally
{
}
}
}
public static void SetWebProperties(SPSite site, string url, string name)
{
if (site != null)
{
try
{
site.PortalUrl = url;
site.PortalName = name;
Console.WriteLine("site: " + site.Url + " was configured successfully");
}
catch (Exception e)
{
Console.WriteLine("error: " + site.Url + " " + e.Message);
}
finally
{
//Ensure SPSite object is disposed.
site.Dispose();
}
}
else
{
//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("");
Console.WriteLine("");
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\" / ");
}
}
}