I'm working on a user control that should show all site names, within a given site, for the current FBA user. My site is structured using SP Groups, FBA Roles and FBA users, i.e.

'Harry' is a member of the 'CSAuthors_EN' role, and
'Melvin' is a member of the 'CSAuthors_FR' role and
both these roles are members of the 'CS - Authors' group.
The site that uses this group is: /customers/Authors


'Brandi' is a member of the 'CSAdmins_EN' role, and
'Melvin' is a member of the 'CSAdmins_FR' role, and
both these roles are members of the 'CS - Admins' group.
The site that uses this group is: /customers/Admins

given that I know 'Melvin' is logged into the site (SPContext.Current.Web.CurrentUser), I want to get a list that shows

Sites for 'Melvin'

To start I'm using SQL to query the FBA aspnet_UsersInRoles database for all the roles for the current user where the rolename starts with "CS" (i.e. CSAUthors, or CSAdmins). From this I know the RoleName. My thought is that I can then look at each sub-site within /customers/ for a site that has permissions for one of the roles, i.e. the /customers/Admins site has the group "CS - Admins", which has a member of the "CSAdmins_FR" role.

Sounds convoluted, I know - is there a better way? Can my idea even work? I'd appreciate any guidance at all


1 Answer 1


Recursion is the answer to this my friend.

private StringCollection sc = new StringCollection();

private void CheckPermissionsOnWeb(SPWeb web, string groupName)
   foreach (SPGroup group in web.Groups)
      if (group.Name == groupName)
         sc.Add(web.Url); // Or web.Name or whatever your custom logic is

   foreach (SPWeb child in web.Webs)
      CheckPermissionsOnWeb(child, groupName);

You just need some explicit logic to map "CSAuthors_*" to "CS - Authors".

  • You just made me excited to get to work tomorrow so i can try this out! I'll let you know how it goes ;)
    – QMKevin
    Commented Nov 20, 2012 at 1:54
  • I would have used Stack(DFS)/Queue(BFS) for this purpose, using recursion may be quite a bit time consuming for large number of nested subsite and when the code will be executed at home page or any other page which user visits (this i assume) you should consider the running time
    – Diptarag
    Commented Nov 20, 2012 at 7:27
  • Can you elaborate on how DFS or BFS could be used in this case? I'm sure nested foreach or do while loops could be just as time consuming, but perhaps I'm looking at this wrong?
    – QMKevin
    Commented Nov 20, 2012 at 12:50
  • 1
    Thanks for the answer rj. Recursion was indeed the answer, with a little manipulation. Cheers!
    – QMKevin
    Commented Nov 21, 2012 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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