Access denied with SPSite.AllWebs

When you require a collection containing all the SPWeb objects in a specific site collection, the SPSite.AllWebs property seems to be sufficient. This statement is only correct as long as the current user is a site collection administrator..

but when the code is executed in the context of a user in the Member or Visitor group, an Access Denied message appears!

If you are not sure that the functionality is exclusive for administrators, try to avoid the following code:


Instead I recommend the following approach by using the GetSubwebsForCurrentUser :

private static IList GetAllWebsSafely(){
    var allwebs = SPContext.Current.Web.Site.RootWeb.GetSubwebsForCurrentUser().ToList();
    return allwebs;

In my case I needed an equivalent of the AllWebs, including the rootweb object. Therefor the rootweb is also added to the collection.

Hope this helps,

VN:F [1.9.22_1171]
Rating: 7.6/10 (8 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 4 votes)
Access denied with SPSite.AllWebs, 7.6 out of 10 based on 8 ratings