in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Another Hack's SharePoint Experiences

I've been playing with SharePoint for a few years now. Every now and again I'll post something that I found interesting about SharePoint or computing in general.

Tip o' the day: Getting the Content database information of a specific site

Here's a way to get the server and database name of the content database that a site belongs to using C#:

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administrator;

public class Test

{

private static SPGlobalAdmin oGlobAdmin = new SPGlobalAdmin();

public static SPVirtualServer GetVirtualServerBySite(SPSite oSite)
  { 
   foreach(SPVirtualServer iServer in oGlobAdmin.VirtualServers)
   {    
    foreach(SPSite iSite in iServer.Sites)
    {
     if(iSite.ID == oSite.ID)
     {
      return iServer;
     }
    }
   }
   throw new IndexOutOfRangeException("Unable to find site in configuration database: " + oSite.Url);
  }

public static void Main(String[] Args)

{

SPSite oSite = new SPSite([Your site's URL here]);

SPVirtualServer oVS = GetVirtualServerBySite(oSite);

string SPContentDBServer = oVS.ContentDatabases[0].Server;
string SPContentDB = oVS.ContentDatabases[0].Name;

Console.WriteLine(SPContentDBServer);

Console.WriteLine(SPContentDB);

}

}

Hopefully someone finds this the least bit interesting...Personally I'm trying to recreate the connection string for the SharePoint DB, but shhhhh, don't tell anyone.  Mucking w/ the SharePoint databases is not supported by MS.

Comments

No Comments

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts