1

I need to make a list for all the users and the permissions that they have in the site contents inside a site collection.
Since all users are getting permissions through SP groups, I need to know the permissions of every SP group.
Practically, I'm trying to get programmatically, the information shown in page:

myserver/_layouts/15/viewgrouppermissions.aspx?ID=9999

I could scrape out the page but I'm looking for a more elegant solution.

0

I found two solutions on this site: Accessing Group Permissions From PowerShell Using Reflection

One solution is with powershell and other one with C#.

Both methods use reflection so you should not use it in a production system, in other environments use it at your own risk.

# inject fake context 
$site = Get-SPSite("http://intranet.contoso.com") 
$web = $site.OpenWeb() 
$request = New-Object System.Web.HttpRequest("", $web.Url, "") 
$response = New-Object System.Web.HttpResponse(New-Object System.IO.StreamWriter(New-Object System.IO.MemoryStream)) 
$dummyContext = New-Object System.Web.HttpContext($request, $response) 
$dummyContext.Items["HttpHandlerSPWeb"] = [Microsoft.SharePoint.SPWeb]$web 
[System.Web.HttpContext]::Current = $dummyContext 
$groupPermissions = New-Object Microsoft.SharePoint.WebControls.GroupPermissions 
# set your group here 
$groupId = $web.SiteGroups["Team Site Owners"].ID 
$groupPermissions.GroupId = $groupId 
# set dummy Page 
$groupPermissions.Page = New-Object System.Web.UI.Page 
# invoke private CreateDataTable method 
$groupPermissionsType = $groupPermissions.GetType() 
$bindingFlags = [System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance 
$mi_CreateDataTable = $groupPermissionsType.GetMethod("CreateDataTable", [System.Reflection.BindingFlags]($bindingFlags)) 
$dataTable = $mi_CreateDataTable.Invoke($groupPermissions, $null) 
#process results 
$regExpPattern = [regex]'<span dir="ltr">(?<scopeUrl>.*?)</span>' 
$dataTable | % { 
  $scopeUrl = $regExpPattern.match($_.ScopeUrl).groups[1].value 
  Write-Host [$scopeUrl] [$_.Role] 
}

This is the C# version:

using (SPSite site = new SPSite("http://intranet.contoso.com"))
{
    using (SPWeb web = site.OpenWeb())
    {
        HttpRequest request = new HttpRequest(string.Empty, web.Url, string.Empty);
        HttpResponse response = new HttpResponse(new System.IO.StreamWriter(new System.IO.MemoryStream()));
        HttpContext dummyContext = new HttpContext(request, response);
        dummyContext.Items["HttpHandlerSPWeb"] = web;
        HttpContext.Current = dummyContext;
        var groupPermissions = new GroupPermissions();
        int groupId = web.SiteGroups["Team Site Owners"].ID;
        groupPermissions.GroupId = groupId;
        groupPermissions.Page = new Page();
        var groupPermissionsType = groupPermissions.GetType();
        var mi_CreateDataTable = groupPermissionsType.GetMethod("CreateDataTable", BindingFlags.NonPublic | BindingFlags.Instance);
        DataTable results = mi_CreateDataTable.Invoke(groupPermissions, null) as DataTable;
        if (results != null)
        {
            foreach (DataRow row in results.Rows)
            {
                string rawScopeUrl = row["ScopeUrl"] as string;
                string scopeUrl = rawScopeUrl;
                string regExpPattern = "<span dir=\"ltr\">(?<scopeUrl>.*?)</span>";
                Regex regex = new Regex(regExpPattern);
                Match match = regex.Match(rawScopeUrl);
                if (match.Success)
                {
                    scopeUrl = match.Groups["scopeUrl"].Value;
                }
                Console.WriteLine("[{0}] – [{1}]", scopeUrl, row["Role"]);
            }
        }
    }
}
  • This code misses ths URL part. It shows the overall permissions of a SP group in a site – XristosK Aug 12 '15 at 11:20
  • @XristosK answer edited – Nk SP Aug 12 '15 at 11:38
  • That's crazy code man. I've troubles translating to c# – XristosK Aug 12 '15 at 13:32
  • Welcome to the reflection world lol! btw in the link that I provided to you, there is also the C# version (look the previous post of the blog) – Nk SP Aug 12 '15 at 13:37

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.