Sometimes it is necessary to do some operations on each web application that is created in the farm. This code-fragment could be used to create this loop:

SPFarm farm = SPFarm.Local;

SPServiceCollection services = farm.Services;

foreach (SPService service in services)
{
 if (service is SPWebService)
 {
  SPWebService webService = (SPWebService)service;

  foreach (SPWebApplication webApp in webService.WebApplications)
  {
   if (webApp.IsAdministrationWebApplication == false) // skip central administration
   {
    // do your operation on the web application right here
   }
  }
 }
}
Advertisements