Step 1: Create web application class to store details about web application like name, owner details.
- public class WebApplication
- {
- public string Name { get; set; }
- public string Value { get; set; }
- public List<NameValuePair> SiteCollections { get; set; }
- public string Owner { get; set; }
- public string Name_Owner
- {
- get
- {
- return string.Format("{0} ({1})", this.Name, this.Owner);
- }
- }
-
- public WebApplication()
- {
- this.Name = this.Value = string.Empty;
- this.SiteCollections = new List<NameValuePair>();
- }
-
- public WebApplication(string name)
- : this()
- {
- this.Name = this.Value = name;
- }
- public WebApplication(string name, string value)
- : this()
- {
- this.Name = name;
- this.Value = value;
-
- }
- }
Step 2: C# code to find All Web Applications.
- public class SharePointInformation
-
- private List<WebApplication> _allWebApplications;
-
- public List<WebApplication> GetAllWebApplication
- {
- get
- {
- if (_allWebApplications == null)
- {
- List<WebApplication> apps = new List<WebApplication>();
-
- try
- {
-
- SPServiceCollection services = SPFarm.Local.Services;
- foreach (SPService service in services)
- {
- if (service is SPWebService)
- {
- SPWebService webservice = (SPWebService)service;
- WebApplication webApps = new WebApplication();
- foreach (SPWebApplication webapp in webservice.WebApplications)
- {
- foreach (SPAlternateUrl spWebAppAlternateURL in webapp.AlternateUrls)
- {
- if (spWebAppAlternateURL.UrlZone == SPUrlZone.Default)
- {
- webApps = new WebApplication(webapp.Name, spWebAppAlternateURL.Uri.AbsoluteUri);
- foreach (string site in webapp.Sites.Names)
- {
- if (site == "")
- {
- webApps.SiteCollections.Add(new NameValuePair() { Name = "/", Value = "/" });
- }
- else
- webApps.SiteCollections.Add(new NameValuePair() { Name = site, Value = site });
- }
-
- apps.Add(webApps);
- }
- }
- }
- }
- }
-
- this._allWebApplications = apps;
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- return this._allWebApplications;
- }
-
-
- set
- {
- this._allWebApplications = value;
- }
- }