Find All Web Application Using SPFarm Services

Step 1: Create web application class to store details about web application like name, owner details.

  1. public class WebApplication  
  2. {  
  3.     public string Name { get; set; }  
  4.     public string Value { get; set; }  
  5.     public List<NameValuePair> SiteCollections { get; set; }  
  6.     public string Owner { get; set; }  
  7.     public string Name_Owner  
  8.     {  
  9.         get  
  10.         {  
  11.             return string.Format("{0} ({1})"this.Name, this.Owner);  
  12.         }  
  13.     }  
  14.   
  15.     public WebApplication()  
  16.     {  
  17.         this.Name = this.Value = string.Empty;  
  18.         this.SiteCollections = new List<NameValuePair>();  
  19.     }  
  20.   
  21.     public WebApplication(string name)  
  22.         : this()  
  23.     {  
  24.         this.Name = this.Value = name;  
  25.     }  
  26.     public WebApplication(string name, string value)  
  27.         : this()  
  28.     {  
  29.         this.Name = name;  
  30.         this.Value = value;  
  31.   
  32.     }  
  33. }  
Step 2: C# code to find All Web Applications. 
  1. public class SharePointInformation  
  2.   
  3.    private List<WebApplication> _allWebApplications;  
  4.      
  5.    public List<WebApplication> GetAllWebApplication  
  6.    {  
  7.        get  
  8.        {                 
  9.            if (_allWebApplications == null)  
  10.            {  
  11.                List<WebApplication> apps = new List<WebApplication>();  
  12.   
  13.                try  
  14.                {  
  15.   
  16.                    SPServiceCollection services = SPFarm.Local.Services;  
  17.                    foreach (SPService service in services)  
  18.                    {  
  19.                        if (service is SPWebService)  
  20.                        {  
  21.                            SPWebService webservice = (SPWebService)service;  
  22.                            WebApplication webApps = new WebApplication();  
  23.                            foreach (SPWebApplication webapp in webservice.WebApplications)  
  24.                            {  
  25.                                foreach (SPAlternateUrl spWebAppAlternateURL in webapp.AlternateUrls)  
  26.                                {  
  27.                                    if (spWebAppAlternateURL.UrlZone == SPUrlZone.Default)  
  28.                                    {  
  29.                                        webApps = new WebApplication(webapp.Name, spWebAppAlternateURL.Uri.AbsoluteUri);  
  30.                                        foreach (string site in webapp.Sites.Names)  
  31.                                        {  
  32.                                            if (site == "")  
  33.                                            {  
  34.                                                webApps.SiteCollections.Add(new NameValuePair() { Name = "/", Value = "/" });  
  35.                                            }  
  36.                                            else  
  37.                                                webApps.SiteCollections.Add(new NameValuePair() { Name = site, Value = site });  
  38.                                        }  
  39.   
  40.                                        apps.Add(webApps);  
  41.                                    }  
  42.                                }  
  43.                            }  
  44.                        }  
  45.                    }  
  46.   
  47.                    this._allWebApplications = apps;  
  48.                }  
  49.                catch(Exception ex)  
  50.                {  
  51.                    MessageBox.Show(ex.Message);  
  52.                }  
  53.            }  
  54.            return this._allWebApplications;  
  55.        }  
  56.   
  57.   
  58.        set  
  59.        {  
  60.            this._allWebApplications = value;  
  61.        }  
  62.    } 
Ebook Download
View all
Learn
View all