Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Server Technologies
SDK Documentation
SPSite Class
SPSite Properties
 AllWebs Property
SPSite.AllWebs Property (Microsoft.SharePoint)
Gets the collection of all Web sites that are contained within the site collection, including the top-level site and its subsites.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)

Visual Basic (Declaration)
Public ReadOnly Property AllWebs As SPWebCollection
Visual Basic (Usage)
Dim instance As SPSite
Dim value As SPWebCollection

value = instance.AllWebs
C#
public SPWebCollection AllWebs { get; }

Property Value

An SPWebCollection object that represents the Web sites.

Best practice is to dispose explicitly of individual Web sites that are retrieved from the collection that is returned through the AllWebs property.

The following code example displays in a console application the number of Web sites in a site collection and their URLs.

Visual Basic
Dim siteCollection As New SPSite("http://" + System.Environment.MachineName)
Dim websiteCollection As SPWebCollection = siteCollection.AllWebs

Console.WriteLine("Count: {0}", websiteCollection.Count) 

siteCollection.Dispose()
C#
using(SPSite oSiteCollection = new SPSite("http://" + System.Environment.MachineName))
{
    SPWebCollection collWebsites = oSiteCollection.AllWebs;
    Console.WriteLine("Count: {0}", collWebsites.Count);

    foreach (SPWeb oWebsite in collWebsites)
    {
        Console.WriteLine("Web site: {0}", oWebsite.Url);
        oWebsite.Dispose();
    }
}
NoteNote:

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Best Practices: Using Disposable Windows SharePoint Services Objects.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Use GetSubwebsForCurrentUser() to avoid Access Denied error      cakriwut   |   Edit   |  

Be carefull when you decide to use AllWebs property of SPSite. Some user that doesn't have access to all site might receive Access Denied error. So , I suggest to use GetSubwebsForCurrentUser() method of SPWeb.

For example look at this following code

  StringBuilder sb = new StringBuilder();
foreach(SPWeb web in SPContext.Current.Site.AllWebs)
{
sb.AppendFormat("Title : {0}<br>",web.Title);
}

For user who has access to all subsite, the code will be fine. Unfortunatelly, for some users who only has access to some subsite, they will receive Access Denied error.

Therefore change to code into

 StringBuilder sb = new StringBuilder();
foreach(SPWeb web in SPContext.Current.Web.GetSubwebsForCurrentUser())
{
sb.AppendFormat("Title : {0}<br>",web.Title);
}

And no more access denied problem.

-Riwut Libinuko-

Tags What's this?: Add a tag
Flag as ContentBug
Use GetSubwebsForCurrentUser() to avoid Access Denied error      Evgeny Zyuzin   |   Edit   |  

Since method AllWebs also returns object for root SPWeb for Site. It will be more correct to look so.

StringBuilder sb = new StringBuilder();

SPWeb rootWeb = SPContext.Current.Site.RootWeb;

sb.AppendFormat("Title : {0}<br>", rootWeb .Title);

foreach(SPWeb web in rootWeb.GetSubwebsForCurrentUser() )

{

sb.AppendFormat("Title : {0}<br>",web.Title);

}

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker