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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s