Archive

Archive for June, 2012

Programmatically get the server roles in SharePoint farm

Hi Friends,

While working with Microsoft SharePoint farm we may need to get different server roles it is playing in farm.

Like Application server, Database server, Web Front End server etc.

Following code will show you how we can get the server role using SharePoint object model-

For an example I have created a console application and added referenceĀ of ‘Microsoft.SharePoint.dll’ and using

‘Microsoft.SharePoint.Administration’

SPFarm spFarm = SPFarm.Local;
SPServerCollection allServers = spFarm.Servers;
if (allServers != null && allServers.Count > 0)
{
   foreach (SPServer eachServer in allServers)
   {
      switch (eachServer.Role)
      {
	case SPServerRole.WebFrontEnd: Console.WriteLine("WFE:: "+eachServer.Address); break;
	case SPServerRole.Application: Console.WriteLine("APP:: " + eachServer.Address); break;
	case SPServerRole.SingleServer: Console.WriteLine("Single:: " + eachServer.Address); break;
	case SPServerRole.Invalid: Console.WriteLine("Invalid:: " + eachServer.Address); break;
      }
   }
}

Happy codingā€¦

Advertisements