Friday, February 29, 2008

How to Enumerate the Shared Service Providers using SharePoint Object Model.

SharePoint Object Model does not expose the SharedResourceProviders collection as a public property. It is a non-public member of the ServerFarm object.

This said, we can still access the SharedResourceProviders property using the magic of "reflection". Below is the code to enumerate the Shared service propviders.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server;

namespace Velavan.EnumSSPs
{
class Program
{
static void Main(string[] args)
{
EnumSharedServiceProviders();
}

private static void EnumSharedServiceProviders()
{
//Get the default server context
ServerContext sc = ServerContext.Default;

//Use reflection to get the m_ServerFarm Property
object serverFarm = sc.GetType().GetField("m_ServerFarm", BindingFlags.Instance BindingFlags.NonPublic).GetValue(sc);

//Use reflection to get the ShareResourceProviders collection
object sharedResourceProviders = serverFarm.GetType().GetProperty("SharedResourceProviders").GetValue(serverFarm, null);

//Enumerate all Shared Service providers
foreach (object sharedResourceProvider in sharedResourceProviders as IEnumerable)
{
Console.WriteLine("SSP - " +
sharedResourceProvider.GetType().GetProperty("Name").GetValue(sharedResourceProvider, null) as string);
}
}
}
}


make sure you add the references to
Microsoft.Office.Server.dll and
Microsoft.SharePoint.dll