Retrieving the SSP Database connection string using Reflection!

I’m currently working on a project in which it’s required to dynamically retrieve the Connection String of a Web Application’s Shared Services database. Unfortunately, this is not easily accessible via the object model as the desired properties are internal.

The task was tricky and it took me sometime to figure out how to accomplish it and the only solution was “Reflection”!

Below is the method I have came up with:

public static string GetSSPConnectionString(SPWebApplication webApp)
{
    try
    {
        ServerContext serverContext = ServerContext.GetContext(webApp);

        Type serverContextType = serverContext.GetType();
        // Get the value of the field m_ServerFarm;                
        SPPersistedObject serverFarm = (SPPersistedObject)serverContextType.
        GetField("m_ServerFarm", BindingFlags.Instance | BindingFlags.NonPublic).
        GetValue(serverContext);
        // Now we need to retrieve a collection of all the Shared Services Providers
        // public SharedResourceProviderCollection SharedResourceProviders { get; }
        PropertyInfo sharedSerivesProvidersProperty = serverFarm.GetType().
        GetProperty("SharedResourceProviders");
        IEnumerable sharedServicesProviders = (IEnumerable)
        sharedSerivesProvidersProperty.GetValue(serverFarm, null);

        Type sharedServiceProviderType = Assembly.GetAssembly(serverContextType).
        GetType("Microsoft.Office.Server.Administration.SharedResourceProvider");
        PropertyInfo webApplicationsProperty = sharedServiceProviderType.
        GetProperty("WebApplications");

        object serviceDB=null;
        foreach (SPPersistedObject sharedSericeProvider in sharedServicesProviders)
        {
            IEnumerable webApplications = (IEnumerable)webApplicationsProperty.
            GetValue(sharedSericeProvider, null);
            // Iterate through all the web applications associated with the current SSP.
            foreach (SPWebApplication webAppAssociatedWithSSP in webApplications)
            {
                if (webApp.Id == webAppAssociatedWithSSP.Id)
                {
                    serviceDB = sharedSericeProvider.GetType().GetProperty
                    ("ServiceDatabase").GetValue(sharedSericeProvider, null);
                    return ((Microsoft.SharePoint.Administration.SPDatabas(serviceDB)).
                    DatabaseConnectionString);                
                }
            }                    
        }
        return String.Empty;                  
    }
    catch (Exception ex)
    {
        ReportsLogger.GetInstance().LogException(ex);
        return String.Empty;
    }
    finally
    {
        ReportsLogger.GetInstance().Debug("Exiting method");
    }

}

 

You can also download the method above from here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s