Tuesday, January 22, 2013

Programmatically obtain a list of SharePoint Web Applications located under a SharePoint Farm

If you ever need to programmatically obtain a list of web applications contained under your SharePoint Farm, here is some code that will do just that...

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    SPFarm farm = SPFarm.Local;
    SPWebService service = farm.Services.GetValue("");

    foreach (SPWebApplication webApp in service.WebApplications)
    {
        // The following code will need to be changed depending on your situation
        ComboBoxItem webAppItem = new ComboBoxItem(webApp.DisplayName, webApp.Id);
        cbWebApp.Items.Add(webAppItem);
    }
});

NOTE: In this particular instance, I'm writing my list of web applications to a combo box control of a Windows application. The idea is that this combo box control will be the first of several chained combo boxes that will allow you to dive deeper into the SharePoint objects based on the items selected in the combo box controls (i.e. Web Application -> Site Collection -> Site -> Document Library -> Document).  Perhaps this my inspire some ideas of how or why this code would be useful.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.