(In response to a tweet conversation between @MattBremer and @mrackley)

The question was – how do you find all the web apps on a SharePoint server, from the server itself.  I have some old code from SharePoint 2003 that did this and so I figured I’d drop hit here – for REFERENCE only.  It’s ugly and has a lot of stuff that should be added to it, but it was a demo/sampler project I had worked on years ago. 

   1: public ArrayList GetAllServerWebs (bool IncludePortal)

   2: {

   3:     ArrayList serverWebs = new ArrayList();

   4: 

   5:     sih.ImpersonateServiceIdentity();

   6:     SPGlobalAdmin globAdmin = new SPGlobalAdmin();

   7:     SPVirtualServerCollection vServers = globAdmin.VirtualServers;

   8: 

   9:     foreach (SPVirtualServer vs in globAdmin.VirtualServers)

  10:     {

  11:         try // protect for virt server problems.

  12:         {

  13:             if (vs.State == SPVirtualServerState.Ready)

  14:             {

  15:                 SPSiteCollection sites = vs.Sites;

  16: 

  17:                 foreach (SPSite site in sites)

  18:                 {

  19:                     try // Protect from site problems

  20:                     {

  21:                         foreach (SPWeb web in site.AllWebs)

  22:                         {

  23:                             if (!isPortalWeb(web) | (isPortalWeb(web) & IncludePortal))

  24:                                 serverWebs.Add(web);

  25:                         }

  26:                     }

  27:                     catch (Exception) {}

  28:                 }

  29:             }

  30:         }

  31:         catch (Exception) {}

  32:     }

  33: 

  34:     sih.RevertFromServiceIdentity();

  35:     return serverWebs;

  36: }

 

I really need to go back and change this from Legacy code to something that uses the SharePoint 2007 updates and does the disposes correctly.