Uncategorized

Get Farm Details using Visual Web part in SharePoint 2010


One of my new requirements on the SharePoint is to show the detail of farm on any web page which can show the site view and the hierarchical view of all the contents in the farm. What are the contents, of the farm, how many list, site, and web applications have been created in my farm which can use further.

I created a visual web part in this lab to accomplish this task, however there are so many alternative to complete this task,

Due to SharePoint internal security threads we can’t use any sand box solution to accomplish this task, fortunately I took update of the Visual studio template and found the viaul web part as sandbox… I will explore the lab of visual web part in further.

Create an Empty SharePoint Project on visual studio.

empty SP project....

empty SP project....

AS we are accessing a farm detail so we  have to deployed it as the Farm based solution no as sandbox😛 thank you SharePoint to Isolate the process flow which make life easier from developer to administrator.

as farm solution

as farm solution

Add Visual Webpart…named as FarmWebPat_Lab. Open  Farm_WebPart.ascx and add table with four button with the following name.

(1) GetFarmDetail

(2) renderWebInfo

(3) renderWebInfoElevated

(4) accessProhibitedNamespace

Get Farm Detail and Add it to the Literal

void GetFarmDetail_Click(object sender, EventArgs e)

{

SPFarm thisfarm = SPFarm.Local;

foreach (SPService svc in thisfarm.Services)

{

if (svc is SPWebService)

{

SPWebService WebSvc = (SPWebService)svc;

foreach (SPWebApplication weapp in WebSvc.WebApplications)

{

LiteralControl _FaramDetail = new LiteralControl();

_FaramDetail.Text += “<br/>”+weapp.ApplicationPool.ToString()+”<br/>”;

this.Controls.Add(_FaramDetail);

}

}

}

}

//Brings sub websites on the Farm Root Web

public void renderWebInfo_Click(object sender, EventArgs e)

{

try

{

SPWeb thisweb = SPContext.Current.Web;

string message = string.Format(“This web contains {0} subwebs”, thisweb.Webs.Count);

LiteralControl _FaramDetail = new LiteralControl();

_FaramDetail.Text = message;

}

catch (Exception ex)

{ }

}

//Test RunWithElevatedPrivileges

public void renderWebInfoElevated_Click(object sender, EventArgs e)

{

try {

SPSecurity.RunWithElevatedPrivileges(ShowWebCount);

}

catch (Exception ex)

{ output.Text = ex.Message; }

}

public void ShowWebCount()

{

SPWeb thisWeb = SPContext.Current.Web;

string message = string.Format(“This web contains {0} subwebs”, thisWeb.Webs.Count);

LiteralControl _FaramDetail = new LiteralControl();

_FaramDetail.Text = message;

}

public void  accessProhibitedNamespace_Click (object sender, EventArgs e)

{

try

{

System.Net.HttpWebRequest.Create(

http://intranet.contoso.com&#8221;);

output.Text = “Success”;

}

catch (System.Security.SecurityException secEx)

{

LiteralControl _FaramDetail = new LiteralControl();

_FaramDetail.Text = “Security Violation! Error caught by my code: ”

+ secEx.Message;

}

catch (Exception ex)

{

LiteralControl _FaramDetail = new LiteralControl();

_FaramDetail.Text = “Generic Exception. Error caught by my code: ”

+ ex.Message;

}

}

//Build and deploy the web part.

getservicesdetails

getservicesdetails

//add the web part and use it to monitor the content in the farm.

addingwebpart

addingwebpart

 

 

//another approach with detail…..

SPFarm thisFarm = SPFarm.Local; //get local farm instance

foreach (SPService svc in thisFarm.Services)

{

String _Detail = “Farm Service (Type=” + svc.TypeName + “; Status=” +

svc.Status + “)”; //shows services in the farm

 

if (svc is SPWebService)

{

SPWebService webSvc = (SPWebService)svc;

foreach (SPWebApplication webApp in webSvc.WebApplications)

{

_Detail += webApp.DisplayName;

if (!webApp.IsAdministrationWebApplication)

{

foreach (SPSite site in webApp.Sites)

{

try

{

_Detail += site.Url;

_Detail +=site.RootWeb.Title;

_Detail +=site.RootWeb.Url;

_Detail += site.RootWeb.ID;

foreach (SPList list in site.RootWeb.Lists)

{

_Detail +=list.Title;

_Detail +=  site.RootWeb.Url ;

_Detail += list.ID;

}

finally

{

site.Dispose();

}

}

}

}

}

}

 

}

 

 

Standard

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