1

I can get to the document library folder/item like this:

SPDocumentLibrary docList = (SPDocumentLibrary)web.Lists["Documents"];
string caml = "<Where><Eq><FieldRef Name='ID' /><Value Type='Integer'>" + id + "</Value></Eq></Where>";
SPQuery qry = new SPQuery();
qry.Query = caml;
SPListItem fldrItem = null;
SPListItemCollection fldrColl = docList.GetItems(qry);
if (fldrColl.Count > 0)
{
    fldrItem = fldrColl[0];
}

This is not a complete example, but what I want to do is to determine what groups have what permission on that fldrItem, add it to a dictionary object in the form of "group", "permission", and eventually I'll write that dictionary out into a DataTable and bind that to a GridView. But, so, inside that last bracket, I want to do something like:

bool hasPerms = false;
Dictionary<string, string> permsDict = new Dictionary<string, string>();
foreach (SPGroup group in web.Groups)
{
    SPBasePermissions perms = SPBasePermissions.EditListItems;
    if (fldrItem.DoesUserHavePermissions(group, perms)) // <-- has to use SPUser, not SPGroup, so how do I check group permissions?
    {
        if (docList.WriteSecurity == 4) //None
        {
            hasPerms = false;
            permsDict.Add(group.Name, "None");
        }
        else if (docList.WriteSecurity == 2) //Only their own
        {
            if (fldrItem["Creator"].ToString() == SPContext.Current.Web.CurrentUser.LoginName)
            {
                hasPerms = true;
                permsDict.Add(group.Name, "Contribute");
            }
        }
        else
        {
            hasPerms = true;        
            if (fldrItem.DoesUserHavePermissions(group, SPPermissionBase.FullMask))
                permsDict.Add(group.Name, "Full Control"); 
        }
}

That 5th line is where my issue is --- How do I check for a group's permissions on a document library/list item? And is there a better/easier way to do this than the way I'm attempting?

1
if (fldrItem.RoleAssignments.Count > 0) {
    SPRoleAssignmentCollection assignColl = fldrItem.RoleAssignments;
    foreach (SPRoleAssignment assignment in assignColl)
    {
        if(assignment.Member.Name == group.Name)
        {
            //Now you can look into the associated RoleDefinitions from assignment
        }
    }
}
  • Can you elaborate on this a little? I don't have a particular group name... I need to cycle through all the groups on the site, and then get what permission they have. And I've looked at RoleDefinitions, but not really sure how to just write out their role, as opposed to setting one. – vapcguy May 1 '15 at 1:25
  • I figured out what you meant on the if(assignment.Member.Name == group.Name) part. I needed to start my loop through the groups right before that with foreach (SPGroup group in web.Groups). So this helps to get the groups on the item. How do I use RoleDefinitions to get their permission? – vapcguy May 1 '15 at 2:04
  • I figured it out... SPRoleDefinition.Name. – vapcguy May 1 '15 at 2:07
0

Amal helped and I used his code as a start, but this is the full answer. I also switched to getting the folder name instead of ID, that I pass in as fldrName:

using (SPSite site = new SPSite(SPContext.Current.Web.Url)) 
{
    using (SPWeb web = site.OpenWeb("/Docs/")
    {
        SPDocumentLibrary docList = (SPDocumentLibrary)web.Lists["Documents"];
        string caml = @"<Where>
             <Eq>
                 <FieldRef Name='FileLeafRef' />
                 <Value Type='Lookup'>" + fldrName + "</Value>
             </Eq>
       </Where>";
       SPQuery qry = new SPQuery();
       qry.Query = caml;
       SPListItem fldrItem = null;
       SPListItemCollection fldrColl = docList.GetItems(qry);
       Dictionary<string, string> permsDict = Dictionary<string, string>();

       if (fldrColl.Count > 0) // does item exist?
       {
           fldrItem = fldrColl[0];

           if (fldrItem.RoleAssignments.Count > 0) {
               SPRoleAssignmentCollection assignColl = fldrItem.RoleAssignments;
               foreach (SPRoleAssignment assignment in assignColl)
               {
                   foreach (SPGroup group in web.Groups)
                   {
                       if(assignment.Member.Name == group.Name)
                       {
                           foreach (SPRoleDefinition role in roleAssignment.RoleDefinitionBindings)
                           {
                               permsDict.Add(group.Name, role.Name);
                           }
                       }
                   }
                }
            }
        }
        else
        {
            // Could not find item
        }
    } // end web
} // end site
  • One note, and for anyone that wants to use this code in the future, when I'm writing the values in the dictionary back out, if I expect a particular group to have permission to the item, and I try to get the KeyValuePair's value based on it with kvp.Key.Contains('My Group'), I do need to make sure to use a TryGetValue expression, in case that role actually doesn't have permission on the item, after all, and there's no key in the dictionary matching "My Group". – vapcguy May 1 '15 at 6:03

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.