Quantcast
Channel: General – Keith Richie
Viewing all articles
Browse latest Browse all 15

Determining the Configuration Database in a SharePoint 2007 Farm

$
0
0

For whatever reason you may have, you may find the need to identify the Configuration Database in your server farm programmatically.  The problem is that it is not directly exposed for consumption via the object model.  What I mean by this, is that there is no single identifiable property that tells you that the database your enumerating is in fact the Configuration Database.

In V2, it was a simple matter of using the SPConfigDatabase object within the Microsoft.SharePoint.Administration namespace, but with V3, this was marked obsolete, with no reference to  what you can use in V3.  Another issue is that the new object type for the configuration database (SPConfigurationDatabase) is not exposed publicly in the object model.

I’ve seen a few posts that drive you to towards deriving it using crazy nested reflection by first starting with the ProcessIdentity object of the farms TimerService, but I recommend a clearer and simpler approach as follows

// Code Begins
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace SPTest
{
    class Program
    {
        static void Main(string[] args)
        {

            // Get the Database Service class within the farm
            SPDatabaseService dbService = SPFarm.Local.Services.GetValue();

            // Enumerate the service instances
            foreach(SPDatabaseServiceInstance instance in dbService.Instances)
            {
                // enumerate the databases within this service instance
                foreach (SPDatabase database in instance.Databases)
                {
                    Console.WriteLine("Database Server: {0} :: Database Name: {1}",database.Server.Name, database.Name);

                    object oDB = database.GetType();

                    Console.WriteLine("Object Type: {0}", oDB.ToString());

                    // If the objects type is "Microsoft.SharePoint.Administration.SPConfigurationDatabase, you've found the Config DB.
                    if (oDB.ToString().CompareTo("Microsoft.SharePoint.Administration.SPConfigurationDatabase") == 0)
                        Console.WriteLine("You found the config database oDB.ToString(): {0} ",oDB.ToString());
                }
            }
        }
    }
}
// Code Ends
 

HTH!

– Keith



Viewing all articles
Browse latest Browse all 15

Latest Images

Trending Articles





Latest Images