11

I must solve this problem:

I have a list in which there are stored some SPListItem. On this list a group(AllReplyers) has the "Contribute" privilege, so that everybody can add file. When a user add an element, i want this element to be modifiable only by him, and readable by the others..

I think a good way to do that is to "break" the item privileges, lower to reader the AllReplyer and add the single user to "contribute".. How can i do this programmatically??

Thank you very much!!

Ziba Leah
  • 1,987
  • 11
  • 48
  • 73

1 Answers1

16

You can do this like this:

public static void AssignPermissionsToItem(SPListItem item, SPPrincipal obj, SPRoleType roleType)
{
    if (!item.HasUniqueRoleAssignments)
    {
        item.BreakRoleInheritance(false, true);
    }

    SPRoleAssignment roleAssignment = new SPRoleAssignment(obj);
    SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
    roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

    item.RoleAssignments.Add(roleAssignment);
}

SPPrincipal is a SPUser or SPGroup. About SPRoleType you can read here.

Alexander
  • 8,139
  • 2
  • 27
  • 42
  • 4
    To make this method more generic and reusable, pass in a SPSecurableObject instead of SPListItem. This way you can use it to secure SPLists and SPWebs too. Might have to pass in a separate SPWeb to get hold of the SPRoleDefinition though, or you can test the type of the SecurableObject and get it from there after doing a cast to the right type http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurableobject.aspx – Jaap Vossers Feb 02 '12 at 14:20