SharePoint programmatically managing item permissions

Assigning SharePoint List Item level permission programmatically

To add item level permission in an SharePoint List or Library, you need to keep three things in mind:
You have a valid SPUser object in your hand
You have to break the role assignment inheritance for the list
You have to add Role Definition and Role Assignment to the targeted list item
Though above statements looks complicated, don't think much about them – just use following two functions wisely and you are done :)
1. You have SPListItem object in your hand
2. You have a valid SPUser in your hand
First call the below method and pass the SPListItem as the input parameter:

RemoveAllPermissions Function
  1. private static void RemoveAllPermissions(SPListItem CurrentlistItem)
  2.         {
  3.             //The below function Breaks the role assignment inheritance for the list and gives the current list its own copy of the role assignments
  4.             CurrentlistItem.BreakRoleInheritance(true);
  5.             //Get the list of Role Assignments to list item and remove one by one.
  6.             SPRoleAssignmentCollection SPRoleAssColn = CurrentlistItem.RoleAssignments;
  7.             for (int i = SPRoleAssColn.Count - 1; i >= 0; i--)
  8.             {
  9.                 SPRoleAssColn.Remove(i);
  10.             }
  11.         }

Next call the below method and pass the desired parameters:
GrantPermission Function
  1. private static void GrantPermission(SPListItem CurrentListItem, SPWeb oSPWeb, SPRoleType SPRoleType, SPPrincipal SPPrincipal)
  2.         {
  3.             try
  4.             {
  5.                 //Create one Role Definition i.e Full Controls, Contribute rights or Read rights etc.
  6.                 SPRoleDefinition oSPRoleDefinition = oSPWeb.RoleDefinitions.GetByType(SPRoleType);
  7.                 //Create one Role Assignment for the specified SP user or group.
  8.                 SPRoleAssignment oSPRoleAssignment = new SPRoleAssignment(SPPrincipal);
  9.                 //Bind the role definition to the role assignment object created for the user or group.
  10.                 oSPRoleAssignment.RoleDefinitionBindings.Add(oSPRoleDefinition);
  11.                 //Add it to the specified list item.
  12.                 CurrentListItem.RoleAssignments.Add(oSPRoleAssignment);
  13.                 //update the list item so that specified user assignment will have the access.
  14.                 CurrentListItem.Update();
  15.             }
  16.             catch (Exception ex)
  17.             {
  18.                 EventLog.WriteEntry("Error in UAR Initiation Workflow""GrantPermission() : " + ex.Message);
  19.             }
  20.         }


Popular posts from this blog

Using External Content Types with Stored Procedures with Input Parameters

HTTPS Site InfoPath Issue : You do not have permissions to access a web service that provides data required for this form to function correctly

Windows 7 Blue Screen because of ConnectiFy