Thursday, September 3, 2009

C# Example: How to get all sites from SharePoint using SPGlobalAdmin and SPVirtualServer objects.

SPListGallery class is the example to learn how people can easy connect the SharePoint gallery and get fast all sites from there by SharePoint object model.

Run example:

SPListGallery gallery = new SPListGallery("http://localserver/");
gallery.GetSharePointSites();       


//===================================================================
// Developer : Tomek Prentki
// Description : Example to get all sites from SharePoint.
//===================================================================

using System;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Framework.Business.Components
{
   /// <summary>
   /// SPListGallery class is the example to learn how people can easy connect into SharePoint gallery and get fast all sites from there.
   /// The is most fastest solution, but very safe to get all object modal important information's.    
   /// </summary>

   public class SPListGallery
   { 
      private string _locationPath;

      /// <summary>
      /// Before you run the SPListGallery functions, first set the properties.
      /// Example -> http header value  of your site ->
http://localhost/
      /// </summary>
      public string LocationPath
     {
         get{
            return _locationPath;
         }
         set{
           _locationPath = value;
      }

  }

  /// <summary>
  /// Constructor.
  /// </summary>

  public SPListGallery(string siteLocation)
  {
      if(siteLocation.Length > 0){
         LocationPath = siteLocation;
      }
      else
      {
       Console.WriteLine("ERROR SITE LOCATION: To run application you need the site location.");    
      }
  }

  /// <summary>
  /// This method get all sharepoint sites.
  /// </summary>

  public void GetSharePointSites()
  {
      SPSite site = null; //Site object
      Uri url = new Uri(LocationPath); 
   
      SPGlobalAdmin globalAdmin = new SPGlobalAdmin();
      SPVirtualServer  virtualServer = globalAdmin.OpenVirtualServer(url); 
   
      //Get all sites from SharePoint location.
      for(int cnt=0; cnt < virtualServer.Sites.Count;cnt++)
      { 
          try
          {
              site = virtualServer.Sites[cnt];
              foreach (SPWeb web in site.AllWebs)
              {
                  try
                     { 
                         //Get the site and web url.
                         Console.WriteLine("SITE NAME : " + web.Title);
                         Console.WriteLine("SITE URL    : " + web.Url);
                     }
                     catch(Exception ex)
                     {
                         Console.WriteLine("ERROR WEB MESSAGE: " + ex.ToString());
                     }
                     finally
                     {
                         web.Close(); //Close objects
                     }
                 }
          }
          catch(Exception ex)
          {
              Console.WriteLine("ERROR VIRTUAL SERVER MESSAGE: " + ex.ToString());
          }
          finally
          {
              site.Close(); //Close site object
          }
      }
   globalAdmin.Close();  // CloseSPGlobalAdmin object.
  }
 }
}

No comments:

Post a Comment