.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to get Farm Servers through code? in SharePoint 2010

Posted By:Jean Paul       Posted Date: April 25, 2014    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

In this article we have explored the C# Server Object Model code to retrieve Farm Server Information. In the real world scenarios we can use this information to quickly understand the server topology.
 

In this article we can explore the following code:

· Retrieve Name & Type of all Servers in the Farm

Farm

A SharePoint Farm consists of multiple servers with role associated to each. For example a typical 500 user Farm can contain:

1. Web Front End Server

2. Application Server

3. Database Server

4. Email Server

5. Searcy Index Server

Following is a typical output from a Server Farm.

image

Code

Here we are using Server Object Model code to retrieve the farm information.

For getting Farm instance, we can use the SPFarm.Local property.

 

using Microsoft.SharePoint.Administration;

SPFarm farm = SPFarm.Local;

For iterating through the servers, we can use the Servers property of Farm object.

 

foreach (SPServer server in farm.Servers)
{
}

For retrieving the Name, Role, Type we can use the instance properties of SPServer.

· server.Name

· server.Role

· server.TypeName

Following is the console application code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace EnumFarmServers
{
    class Program
    {
        static void Main(string[] args)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPFarm farm = SPFarm.Local;

                foreach (SPServer server in farm.Servers)
                {
                    Console.WriteLine(server.Name);
                    Console.WriteLine(server.Role);
                    Console.WriteLine(server.TypeName);
                    Console.WriteLine();
                }
            });

            Console.ReadKey(false);
        }
    }
}

 

Central Administration

We can always retrieve this information from Central Administration as well.

image

The console application source code is attached.

Squadron

Please note that the above code is integrated with Squadron (Free Administration Tool for SharePoint)

image

You can download the tool from:

http://www.sharepointcto.com/Squadron/Default.aspx

References

http://www.sharepointcto.com/Squadron/Default.aspx

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spserver.aspx

Summary

In this article we have explored the C# Server Object Model code to retrieve Farm Server Information. In the real world scenarios we can use this information to quickly understand the server topology.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend