0

I found this question and that person was asking how to get all the groups from a site, but instead he already has code that returns groups that have permissions on the site. And that is what I am looking for.

So basically, I need to be able to show all the groups that have permissions (such as edit and read permissions) on a site or subsite. I have about 12 subsites and they all have different groups with permissions, and my goal is to show all the groups per subsite.

Here's the code he has in his post:

using (SPSite scSite = new SPSite(fullsite))
            {
                using (SPWeb scWeb = scSite.OpenWeb())
                {
                    foreach(SPGroup spg in scWeb.Groups)
                    {
                        debug += spg.Name + "!!!";
                    }
                }
            }

However, his code doesn't work for me. I've never had to user "SPSite" or "SPWeb" or "SPContext" or "SPGroup". Instead I always managed to get my WinForm to work with just "Site" or "Web" or "Context" or "Group".

I've made a WinForm in C# and I want to make it so that I can select a subsite and it should then show me the groups and their permissions per subsite. I am using a nice combobox with subsites in it and some labels that show me how many pages/libraries/lists and such are present in the selected subsite. The only thing I can't figure out is how to get the groups that have permissions on the subsite.

Any help would greatly be appreciated! I've been searching for this for over a week now and I'm starting to get really desparate.

Code example of my form, to give you an idea of how I use Web and such (this particular piece fills the combobox with my main site (Q-Gids) and its subsites):

public Form1()
    {
        InitializeComponent();
        string url = "https://url-to-my-intranet-site.be";
        using (ClientContext context = new ClientContext(url))
        {
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
            Web web = context.Web;
            context.Load(web);

            context.Load(web.Webs);
            context.ExecuteQuery();
            comboBox1.Items.Add("Q-Gids");

            foreach (Web website in web.Webs)
            {
                comboBox1.Items.Add(website.Title);
            }
        }
    }



EDIT:
I fear I might have misphrased my question a bit, but it turns out that it is solved now. So for those who need to get all the groups in a subsite along with their permissions, here's a piece of code that might be of any help to you:

   using (ClientContext context = new ClientContext(url))
   {
      Web web = context.Web;
      ListCollection pageList = web.Lists;
      RoleAssignmentCollection roleAssignments = web.RoleAssignments;
      context.Load(web.Webs);
      context.Load(web.Lists);
      context.Load(context.Site.OpenWeb(comboValue1), p => p.Url, p => p.Title);
      context.Load(context.Site.OpenWeb(comboValue1).SiteGroups);
      context.Load(context.Site.OpenWeb(comboValue1).RoleAssignments.Groups);
      context.Load(web, p => p.HasUniqueRoleAssignments, p => p.EffectiveBasePermissions, p => p.FirstUniqueAncestorSecurableObject);
      context.Load(roleAssignments.Groups);
      context.ExecuteQuery();
      GroupCollection o1;
      o1 = context.Site.OpenWeb(comboValue1).RoleAssignments.Groups;

      foreach (Group gp2 in o1)
      {
         RoleAssignment oRoleAssignment = web.RoleAssignments.GetByPrincipal(gp2);
         context.Load(gp2);
         context.Load(gp2.Users);
         context.Load(oRoleAssignment, p => p.RoleDefinitionBindings);
         context.ExecuteQuery();
         int counter2 = 0;
         string tempString = "";
         int fixedCounter = oRoleAssignment.RoleDefinitionBindings.Count();
         foreach (RoleDefinition inRole in oRoleAssignment.RoleDefinitionBindings)
         {
            counter2++;
            tempString += inRole.Name.ToString();
            if (counter2 != fixedCounter)
            {
               tempString += ", ";
            }
         }
         ML.Add(new SmallColumnHolder { cGroupName = gp2.Title.ToString(), cGroupPermissions = tempString });
      }
      foreach (var i in ML.ToArray())
      {
         ListViewItem li = new ListViewItem(new string[] { i.cGroupName, i.cGroupPermissions });
         listView1.Invoke((MethodInvoker)delegate { listView1.Items.Add(li); });
      }
   }

comboValue1 is the selected value from a combobox, which holds the name of a subsite. I first add each group from that subsite to a list, and then loop through that list and for each item in it, I add it to a listview. All of this is happening in a background worker, hence the delegate. Thank you everyone who helped me with this. :)

  • Hi Magali: I've responded with a comment regarding the answers from Brandon C but you are basically using the Client-Side Object Model which is why the examples you're seeing look a bit different and why most examples show classes prefixed with "SP": those are examples based on the Server-Side Object Model. Check out this for examples about getting the SharePoint Groups: msdn.microsoft.com/en-us/library/office/… Hope this helps move you on a bit. :) – Steve Jun 11 '14 at 10:23
1

If you are asking how to get group permissions, you use the SPWeb.RoleAssignments function built into sharepoint. Here is a link to the webpage: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.roleassignments.aspx

Then write code to loop through each web in your site collection, grabbing each group in those webs. Let's say you want to write to console each group that has the "Design" role. Then have your code look like:

using (SPWeb web = site.RootWeb)
{
SPRoleAssignmentCollection oRoleAssignments = web.RoleAssignments;
if(oRoleAssignments -eq "Designer") {
      write-host "Group Name with Design Permission: " $oRoleAssignments.Member.Name
}
}

Something like that at least. My code doesn't include looping through all the webs in your site collection, but you get the idea. That's how you reference group roles.

  • But I can't use SPWeb, anything that has SP at the beginning doesn't work. I always used just "Web" instead of "SPWeb", when I use "SPWeb" it gives me the error that the namespace can't be found even though I have "using Microsoft.SharePoint;" at the top of my code... – Magali Jun 11 '14 at 8:32
  • But you said you got it to work without the "SP" part? Maybe you are including a file that uses a namespace overwriting SPWeb and SPSite? – Brandon C. Jun 11 '14 at 8:34
  • I can use Web and ListCollection and Site and ClientContext and such, and I can connect to my SharePoint environment just fine with that, but everything I find online always begins with "SP" like "SPWeb" and that doesn't work. I don't know why. – Magali Jun 11 '14 at 8:37
  • Can you post an example snippet of code to show what you mean? The only way I can think of the terms being changed from SPWeb to just Web is if you are using namespaces to overwrite the term. – Brandon C. Jun 11 '14 at 8:43
  • 1
    Just to be clear Site and Web and ClientContext etc. are part all of the Client-side Object Model and as Magali is using a WinForm it makes sense that CSOM is being used. Magali: you need to look for examples using the Client-side Object Model or CSOM. :) – Steve Jun 11 '14 at 10:15
0

Prints out all the groups for the SPWeb and the group's role names:

using (SPSite site = new SPSite("http://server/sites/sitecollection/"))
{
    using (SPWeb web = site.OpenWeb("test"))
    {
        foreach (SPRoleAssignment roleAssignment in web.RoleAssignments)
        {
            if (roleAssignment.Member is SPGroup)
            {
                Console.WriteLine("Group: " + roleAssignment.Member.Name);
                foreach (SPRoleDefinition roleDefinition in roleAssignment.RoleDefinitionBindings)
                {
                    Console.WriteLine("\t" + roleDefinition.Name);
                }
            }
        }
    }
}
  • The OP seems to be using CSOM, this is server side code – Robert Lindgren Oct 10 '15 at 5:20

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.