Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 11 down vote accepted

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.

share|improve this answer
3  
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/… –  Jaap Vossers Feb 2 '12 at 14:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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