Home > Microsoft SharePoint, Microsoft SharePoint 2010, MOSS2007 > Programmatically get the server roles in SharePoint farm

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
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: