Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / Windows Forms

SharePoint SPSite ListView

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
2 Apr 2010CPOL 13.1K   3  
How To Get All Site Urls Without Creating an SPSite object?

Let’s say you are trying to create a SharePoint admin tool that shows all site collections in the farm, then allows the user to interact with only the site collections of interest to him.

To get all the site collection Urls in the farm, you’d usually go with the following approach:

foreach (SPWebApplication webApp in SPWebService.ContentService.WebApplications)
{
    foreach (SPSite site in webApp.Sites)
    {
        string fullSiteUrl = site.Url;
        Console.WriteLine(fullSiteUrl);
        site.Close();
    }
}

The disadvantage in using such an approach is that we have to create an SPSite object for every site collection in the farm, then close it. That’ll mean a lot of work allocating and deallocating memory. Luckily, there is a better approach to do this, shown below:

foreach (SPWebApplication webApp in SPWebService.ContentService.WebApplications)
{
    string webAppUrl = webApp.GetResponseUri(SPUrlZone.Default).AbsoluteUri; 

    foreach (string siteUrl in webApp.Sites.Names)
    {
        string fullSiteUrl = webAppUrl + siteUrl;
        Console.Writeline(fullSiteUrl);
    }
}

This way, we don’t have to create any of the expensive SPSite objects then close them. On a small farm, that’ll help your application load faster, but on a large farm with hundreds of site collections, this approach is necessary.

You can find the full source for the SPSite ListView here. This was created as a Visual Studio 2008 project on top of SharePoint 2010 Beta, but you can easily remove the reference and add a reference to the 2007 Microsoft.SharePoint.dll.


Filed under: SharePoint Tagged: site collection, site url, SPSite
This article was originally posted at http://mycodelog.com/2010/03/03/spsites

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
https://open-gl.com

Comments and Discussions

 
-- There are no messages in this forum --