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