Setting/Changing item level permissions in SharePoint Event Receiver:
public override void ItemAdded(SPItemEventProperties properties)
{
SPWeb web = properties.Web;
SPListItem listItem = properties.ListItem;
//get the username from list item people picker
string accName = listItem["Name"].ToString();
SPUser user = web.EnsureUser(accName);
listItem.BreakRoleInheritance(false);
SPRoleDefinitionCollection webroledefinitions = web.RoleDefinitions;
SPRoleAssignment roleassignment = new SPRoleAssignment(user);
//Set Read permission
roleassignment.RoleDefinitionBindings.Add(webroledefinitions["Read"]);
listItem.RoleAssignments.Add(roleassignment);
listItem.SystemUpdate(false);
}
public override void ItemAdded(SPItemEventProperties properties)
{
SPWeb web = properties.Web;
SPListItem listItem = properties.ListItem;
//get the username from list item people picker
string accName = listItem["Name"].ToString();
SPUser user = web.EnsureUser(accName);
listItem.BreakRoleInheritance(false);
SPRoleDefinitionCollection webroledefinitions = web.RoleDefinitions;
SPRoleAssignment roleassignment = new SPRoleAssignment(user);
//Set Read permission
roleassignment.RoleDefinitionBindings.Add(webroledefinitions["Read"]);
listItem.RoleAssignments.Add(roleassignment);
listItem.SystemUpdate(false);
}
Comments
Post a Comment