Add Permission for specific User to SharePoint Library Document (Item Level)

Today I got work to add permission for particular SharePoint User on Library Item using event receiver on Item updated event (as it is Document Library we have to write code on item updated to get Document List Item).

SPUser userToAdd =
SPPrincipal principal = (SPPrincipal)userToAdd; // get the principal of that user
SPRoleDefinition rd = properties.Web.RoleDefinitions["Read"]; // assign role of Read means give him Read permission
SPRoleAssignment ra = new SPRoleAssignment(principal);
ra.RoleDefinitionBindings.Add(rd);//assign the role defination to role assignment
properties.ListItem.RoleAssignments.Add(ra);//add role assignment to item;s roleassignment
properties.ListItem.Update();

And do not forget to add EventFiringEnabled = false; before updating the item ๐Ÿ˜€

Advertisements