Programmatically How to get all web application details in SharePoint 2010

In previous post shown how to create new web application using server object model.

Now here the code snippet for get all web applications using server object model in SharePoint 2010


Method 1:


           string strGetAllWebApps = "";
            SPServiceCollection services = SPFarm.Local.Services;
            foreach (SPService service in services)
            {
                if (service is SPWebService)
                {
                    SPWebService webservice = (SPWebService)service;
                    foreach (SPWebApplication webapp in webservice.WebApplications)
                    {
                        foreach (SPAlternateUrl url in webapp.AlternateUrls)
                        {
                            strGetAllWebApps+= url.Uri + "\n";
                        }
                    }
                }
            }
            


Method 2:


            string strGetAllWebApps2 = "";
            SPWebApplicationCollection webapps = SPWebService.ContentService.WebApplications;
            foreach (SPWebApplication webapp in webapps)
            {
                foreach (SPAlternateUrl url in webapp.AlternateUrls)
                {
                        strGetAllWebApps2 += url.Uri + "\n"; 
                }
            }

Comments