Monday, October 07, 2013

Item Level Permission in SharePoint

Item level permission will be set after that item has been created or updated. When you set item level permission on updating the item you need to remove all the permission and then update the custom permission on the clean slate. Its always best to wire up the permission on the item on "List Item Event Receiver". Code snippet for adding new item level permission
                    var list = webSite.Lists["Region"];
                    SPListItem item = list.Items.GetItemById(4);
                    webSite.AllowUnsafeUpdates = true;
                    SPUser user = webSite.EnsureUser(@"Global\dhanyata");
                    item.BreakRoleInheritance(false);
                    SPRoleDefinitionCollection roleDef = webSite.RoleDefinitions;
                    SPRoleAssignment roleAssignment= new SPRoleAssignment(user);
                    roleAssignment.RoleDefinitionBindings.Add(roleDef["MyCustomPermission"]);
                    item.RoleAssignments.Add(roleAssignment);
                    item.Update();   
            
I have created the custom permission level to be set on item. IF you want to create this programmatically,you can choose to work SPBasePermission class and then assigning to your user or group.