In SharePoint, users have the option to share a document or a folder with another user with either Read or Edit permissions. So you can have a document or may be a folder that you have shared with a person, or many person, or even a group. So now the endevaour is to get a list of all the users with whom a given document is shared with their corresponding permission levels.
To do this, we’re take the help of Role Assignment class.
SharePoint stores this information in the ListItem’s RoleAssignments property. So have a look at the following code:
Here, what I am doing is that for a given SharePoint site, I am connecting to a Document Library named, “ListTitle”. Then I am trying to get an item, “WordFileName” from that list as this item has got some unique roles.
The important things to note down here is:
- A single item can have multiple Role Assignments i.e., different types of roles can be assigned to it. Say, a given user, A, might be assigned with “Edit” permission while another user, B, can have only “Read” permissions. Each of this variations will come down as a single Role Assignment(RA).
- A single Role Assignment can have multiple bindings.
Following are the roles defined for the user, that you can get for an item:
- Full Control
- Limited Access
- View Only
- Records Center Web Service Submitters
Similarly, here’s the link to the blog where I have described how, through code, we can share Office365 file, https://realmpksharepoint.wordpress.com/2014/08/04/share-a-sharepoint-file-with-different-office365-users-using-client-object-model-c/
// get the visitors group
// Override default permission inheritance
// Creation role definition
roleCreationInfo.Description="This Role is just for Create Items";
roleCreationInfo.Name="Just Create Items";
// Get principal for usergroup and the contributor role
//var role = GetRole(ctx, RoleType.Contributor);
// Add the RoleCreationDefinition
// Add the role to the collection.