locked
Item Level Permissions RRS feed

  • Question

  • Hello,

    How would I use the SharePoint OM to dynamically set item level permissions? Let's say I have a piece of code like this:

    foreach(SPListItem currItem in currItems)
    {
     // Do some logic
     // Set item level permissions
    // Update List
    }

    Any guidance is appreciated.

    Thanks,
    Igor

    Tuesday, June 7, 2011 9:18 PM

Answers

  • ok. Hopefully this should solve it. 

     

     

    using(SPWeb oWebsite = SPContext.Current.Site.AllWebs["SubSite1"])
    {
    
     SPRoleDefinitionCollection collRoleDefinitions = oWebsite.RoleDefinitions;
    
    foreach(SPListItem currItem in currItems)
    {
     SPRoleAssignmentCollection collRoleAssignments = currItem.RoleAssignments;
    
    SPUser user = oWebsite.EnsureUser("USERA");
    
     SPRoleAssignment oRoleAssignment = new 
    SPRoleAssignment(user.LoginName,user.Email,user.Name,user.Notes);
    
     SPRoleDefinitionBindingCollection collRoleDefinitionBindings = oRoleAssignment.RoleDefinitionBindings;
    
     collRoleDefinitionBindings.Add(collRoleDefinitions["Contribute"]);
    
     collRoleAssignments.Add(oRoleAssignment);
    }
    }
     
    
    

     


    Thanks, Mayur Joshi
    • Edited by Mayur Joshi Wednesday, June 8, 2011 4:51 PM chanegd the role definition name
    • Proposed as answer by Mayur Joshi Wednesday, June 8, 2011 4:52 PM
    • Marked as answer by igor92128 Wednesday, June 8, 2011 5:35 PM
    Wednesday, June 8, 2011 4:51 PM

All replies

  • currItem.BreakRoleInhertiance(boolean, boolean) is the method call to set item level permission on that list item. 
    Thanks, Mayur Joshi
    Tuesday, June 7, 2011 9:23 PM
  • But what if I need to also add let's say write permissions for USERA and read only permissions for USERB?

    Tuesday, June 7, 2011 10:05 PM
  • currItem.BreakRoleInhertiance(false, false) ;
    
    RoleDefinition contribute = objWeb.RoleDefinitions.GetByName("Contribute");
    
    
    
    RoleAssignmentCollection collRoleAssign = currItem.RoleAssignments;
    
    collRoleAssign.Add(web.EnsureUser(USERA), contribute);
    
    

    similarly you can do this for read


    Thanks, Mayur Joshi
    • Proposed as answer by Mayur Joshi Tuesday, June 7, 2011 10:51 PM
    • Marked as answer by Porter Wang Wednesday, June 8, 2011 3:11 AM
    • Unmarked as answer by igor92128 Wednesday, June 8, 2011 4:07 PM
    • Unproposed as answer by Mayur Joshi Wednesday, June 8, 2011 4:46 PM
    Tuesday, June 7, 2011 10:21 PM
  • Almost got it but the Add method in the last line above only accepts an SPRoleAssignment or SPPrincipal.
    Wednesday, June 8, 2011 3:09 PM
  • ok. Hopefully this should solve it. 

     

     

    using(SPWeb oWebsite = SPContext.Current.Site.AllWebs["SubSite1"])
    {
    
     SPRoleDefinitionCollection collRoleDefinitions = oWebsite.RoleDefinitions;
    
    foreach(SPListItem currItem in currItems)
    {
     SPRoleAssignmentCollection collRoleAssignments = currItem.RoleAssignments;
    
    SPUser user = oWebsite.EnsureUser("USERA");
    
     SPRoleAssignment oRoleAssignment = new 
    SPRoleAssignment(user.LoginName,user.Email,user.Name,user.Notes);
    
     SPRoleDefinitionBindingCollection collRoleDefinitionBindings = oRoleAssignment.RoleDefinitionBindings;
    
     collRoleDefinitionBindings.Add(collRoleDefinitions["Contribute"]);
    
     collRoleAssignments.Add(oRoleAssignment);
    }
    }
     
    
    

     


    Thanks, Mayur Joshi
    • Edited by Mayur Joshi Wednesday, June 8, 2011 4:51 PM chanegd the role definition name
    • Proposed as answer by Mayur Joshi Wednesday, June 8, 2011 4:52 PM
    • Marked as answer by igor92128 Wednesday, June 8, 2011 5:35 PM
    Wednesday, June 8, 2011 4:51 PM