0

I have created a Discussion board list inside my site (sp2013) and i want to change item-level permission programmatically.

Actually, i can do this from the User Interface via the "shared with" but my goal is to do it when item(discussion) is being added.

I searched all over the web and all i found was talking about either workflows or REST api (wich i don't master very well)... so please can anyone help me find a way out from this issue?

i have this code but it changes the permission for the hole list not for a specific item

private void AddNewPermission(SPList list, SPWeb objSPWeb, SPRoleType objSPRoleType, SPPrincipal objSPPrincipal)
{
      SPRoleDefinition objSPRoleDefinition = objSPWeb.RoleDefinitions.GetByType(objSPRoleType);
      SPRoleAssignment objSPRoleAssignment = new SPRoleAssignment(objSPPrincipal);
      objSPRoleAssignment.RoleDefinitionBindings.Add(objSPRoleDefinition);
      list.RoleAssignments.Add(objSPRoleAssignment);
}
0

It is identical (SPList, SPListItem and SPWeb all inherit SPSecurableObject). Just first you need to break role inheritance for SPListItem, so that it can have unique permissions. BreakRoleInheritance breaks the permissions and copies them from objects parent (in this case SPList), if you pass true, else it clears all permissions.

private void AddNewPermission(SPListItem listItem, SPWeb objSPWeb, SPRoleType objSPRoleType, SPPrincipal objSPPrincipal)
{
      if (!listItem.HasUniqueRoleAssignments)
      {
          listItem.BreakRoleInheritance(false);
      }
      SPRoleDefinition objSPRoleDefinition = objSPWeb.RoleDefinitions.GetByType(objSPRoleType);
      SPRoleAssignment objSPRoleAssignment = new SPRoleAssignment(objSPPrincipal);
      objSPRoleAssignment.RoleDefinitionBindings.Add(objSPRoleDefinition);
      listItem.RoleAssignments.Add(objSPRoleAssignment);
}

Also keep in mind that you won't be able to do it in itemAdding event, only in ItemAdded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.