1

I need to iterate to all the web of a site collection using the current user permission.
I tried:

foreach (SPWeb web in site.AllWebs)
{
 if (!(web.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.Open))) continue;
}

But it is not working.
I got an error on site.AllWebs

I need all the sites (and subsites) of the site collection. (all the siblings)

3

Option 1. SPWeb.GetSubwebsForCurrentUser method

SPWeb.GetSubwebsForCurrentUser method gets the subsites beneath the current site of which the current user is a member

Since it retrieves only beneath the current site(1 level only), you could use the following method to return all web sites that are contained within the site collection:

private static void GetAllSubwebsForCurrentUser(SPWeb currentWeb, ICollection<SPWeb> allWebs)
{
     allWebs.Add(currentWeb);
     var webs = currentWeb.GetSubwebsForCurrentUser();
     foreach (SPWeb web in webs)
     {
         if (web.GetSubwebsForCurrentUser().Count > 0)
         {
            GetAllSubwebsForCurrentUser(web, allWebs);
         }
         else
         {
             allWebs.Add(web);
         }
     }
 }

Usage

using (var site = new SPSite("http://contoso.intranet.com"))
{
    var allWebs = new Collection<SPWeb>();
    GetAllSubwebsForCurrentUser(site.RootWeb, allWebs);
    foreach (SPWeb web in allWebs)
    {
         Console.WriteLine("Web site:{0} ({1})", web.Title, web.Url);
         web.Dispose();
    }
}

Option 2. SPSite.AllWebs property

Use SPSite.AllWebs property to Gets the collection of all Web sites that are contained within the site collection, including the top-level site and its subsites:

SPSecurity.RunWithElevatedPrivileges(() =>
            {
                using (var site = new SPSite("http://contoso.intranet.com"))
                {
                    var allWebs = site.AllWebs;
                    foreach (SPWeb web in allWebs)
                    {
                        Console.WriteLine("Web site:{0} ({1})", web.Title, web.Url);
                        web.Dispose();
                    }
                }          
            });
  • Please check the answer a little bit carefully, in option 1 you will find the GetAllSubwebsForCurrentUser method that returns ALL web sites for a user that are contained within the site collection – Vadim Gremyachev Sep 8 '14 at 14:50
  • It returns all the web in the same hierarchy of the current web (all parents and children). But what about siblings? – Nk SP Sep 8 '14 at 14:55
  • It returns all the webs within site collection where user has enough permissions, it traverses starting from ROOT web across the all sites within site collection. I dont get about siblings, you mean web sites from different site collections? – Vadim Gremyachev Sep 8 '14 at 14:59
0

You can use following :

    using(SPSite site = new SPSite("http://example/site/"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPWebCollection webCollection = web.GetSubwebsForCurrentUser();
    }
}

This will give you the list of all webs for current user.

  • but in which site collection? – Nk SP Sep 8 '14 at 12:43
  • Check the edited post. – Aanchal Sep 8 '14 at 12:45
  • but I ll get only the sub site of the current Web, but not all the other sites in the site collection – Nk SP Sep 8 '14 at 12:47
  • You will get all sites under the site collection. Try this – Aanchal Sep 8 '14 at 12:49
  • No, I just get the subsite of the current web – Nk SP Sep 8 '14 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.