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!!

1 Answer 1

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.

1
  • 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 msdn.microsoft.com/en-us/library/… Commented Feb 2, 2012 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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