SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a need to recursivily display all subsites which a user only has explicit permissions to view. Trying to avoid pulling back all sites that have All Authenticated Users permissions. I found an excellent example, but I'm still new and trying to figure out permissions.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Collections.Generic;
using SharePoint2010.TestWebParts.WebParts.DisplaySubSiteLinks.Classes;

namespace DisplaySubSiteLinks

class SiteProperties
    public string LinkName { get; set; }
    public string LinkURL { get; set; }
    public string SiteDef { get; set; }

public partial class DisplaySubSiteLinksUserControl : UserControl
    List<SiteProperties> allSiteLinks = null;

    protected override void OnLoad(EventArgs e)
        allSiteLinks = new List<SiteProperties>();
        using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            using (SPWeb rootWeb = site.RootWeb)
                CollectWebProperties(rootWeb, ref allSiteLinks);

        linksData.DataSource = allSiteLinks;

    private static void CollectWebProperties(SPWeb web, ref List<SiteProperties> allSiteLinks)
        allSiteLinks.Add(new SiteProperties()
            LinkName = web.Title,
            LinkURL = web.Url,
            SiteDef = web.WebTemplate

        foreach (SPWeb childWeb in web.Webs)
            CollectWebProperties(childWeb, ref allSiteLinks);


share|improve this question

The code snippet above just returns a collection with properties for the root web (and maybee som sub webs..) of the current web, no matter how the permissions has been granted.

To check permissions for every web you would need to retreive the SPRoleAssignment objects from each web and try to find the current user in it. Though this will not work if the user has been added to a group that has been granted permissions (like the All Authenticated Users e.g.).

You could, though, exclude webs where All Authenticated Users have been given the same rights (or higher) as current user.

This is not the most straight forward task. Since for every SPRoleAssignment, on every single web you would need to first determine if the Principal is a User or a Group. If it is a Group you would then need to check

  1. If the group is All Users Group
  2. If the current user is a member of the group

and if the Principal is a User you would need to check if it is the current user.

Then you would need to save the highest authentication level that the user is granted (directly or from a group if you like) and compare to the highest level that "All Authenticated Users" have been granted.

Does not seem so easy right?

Why not try to use the three "User" members of an SPWeb object, the web.Users, web.AllUsers and web.SiteUsers?

  • web.Users - Returns users that has been granted explicit permissions on the web (but not through a group)
  • web.AllUsers - Returns all users that has access to the site, no mather how it has been granted, BUT only if the user has visited the site
  • web.SiteUsers - Returns all users that belongs to the whole site collection.

So basically, if the user is found in web.Users it has been given direct permissions.

If not found there you would need to check web.AllUsers and if the user is found there, you would need to check if "All Authenticated Users" has the same permission level.. (But if All Authenticated Users and the current user has been granted the same permission level it is getting complicated again, since how would you know if the user is getting itäs permissions from "All Authenticated Users" or from another group?)

I know this is not a complete answer, just wanted to point out som difficulties and fall pits.

share|improve this answer
I think one of the issues i'm going to run into is surrounding All Authenticated Users. We are only wanting to show sites where All Authenticated users is not being used, or it's a unique site. Would GetSubwebsForCurrentUser() be a better fit? – user16650 May 7 '13 at 20:32
Seems promising, but I haven't tested it myself. Give it a shot! – Robert Lindgren May 8 '13 at 5:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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