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. :)