7

I am trying to get the list of user groups specific to a site (within a site collection) but the Web.SiteGroups is returning all the user groups at the site collection level, and therefore I am unable to determine which group has permissions to a specific site.

I need help in below points.

  1. Understand the concept/relationship between the site collection(all sites in the collection) and user groups?
  2. Specific to a site get all the user groups that have some permission to the site and exclude groups that do not have permission to a site/ code is included.

Below is the code:

public static void GetSiteUsers(ClientContext ctx, Web mySite, StringBuilder sb)
    {
        //to retrieve master site groups
        //ctx.Load(mySite.SiteGroups);
        //ctx.ExecuteQuery();

        GroupCollection oSiteCollectionGroups = mySite.SiteGroups; //KT:9-3-15 to limit groups to this site
        //GroupCollection oSiteCollectionGroups = ctx.Web.SiteGroups;
        Console.WriteLine("-----------------Groups in Site--------------------");
        Console.WriteLine(mySite.Title + " List of groups in the site collection");
        sb.AppendLine("-----------------Groups in Site--------------------");
        sb.AppendLine(mySite.Title + " List of groups in the site collection");

        ctx.Load(oSiteCollectionGroups);
        ctx.ExecuteQuery();


        foreach (Group oGroup in oSiteCollectionGroups)
        {
    ***//Unfortunately the oSiteCollectionGroup contains groups from the parent site, as well as the child sites***
            //Console.WriteLine(mySite.Title + " Group :" + oGroup.Title + " id = " + oGroup.Id);
            //sb.AppendLine(mySite.Title + " Group :" + oGroup.Title + " id = " + oGroup.Id);
            ////Console.WriteLine(" Group Description:" + oGroup.Description);
            try
            {
                UserCollection usercollection = oGroup.Users;
                ctx.Load(usercollection);
                ctx.ExecuteQuery();

                foreach (User oUser in usercollection)
                {
                    //Console.WriteLine(mySite.Title + " Group :" + oGroup.Title + " User: " + oUser.LoginName);
                    string[] loginNameParts = oUser.LoginName.Split('|');


                    sb.AppendLine(mySite.ServerRelativeUrl + "," + oGroup.Title + "," + oGroup.Context + "," + loginNameParts[2] + "," + oUser.Email);
                    //sb.AppendLine(web.Title + "," + oUser.LoginName + "," + oGroup.Title);
                }
            }
            catch
           {
                //Console.WriteLine("Site : " + mySite.Title + " Group :" + oGroup.Title + " Group ID:" + oGroup.Id + " User: No Access ");
                //sb.AppendLine("Site : " + web.Title + " Group :" + oGroup.Title + " User: No Access ");
                //sb.AppendLine(web.Title + "," + "No Access to User" + "," + oGroup.Title);
            }


        }


    }
7

Here is how to do it in JS

 var clientContext = SP.ClientContext.get_current();
 var web = clientContext.get_web();
 var webGroupsCollection = web.get_roleAssignments().get_groups(); //gets a  web specific groups
 var siteGroupsCollection = web.get_siteGroups(); //gets all site collection’s groups

similarly there would be something like Web.RoleAssignments.Groups in C# object model

  • How do we do this in PowerShell ? – Nigel Price Aug 29 '17 at 11:15

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.