How to add or remove permissions to a list item in sharepoint

Ok here is how I add or remove or copy permissions to a list item in sharepoint:

1 if (properties.ListTitle.Equals(“Document Library”))
2 {
3 using (SPWeb objWeb = properties.OpenWeb())
4 {
5 SPListItem d_item = properties.ListItem; //this is the one I want to copy permission from
6 SPRoleAssignmentCollection roles = d_item.RoleAssignments;
7 SPListItem item = myFile.Item; //this is the one I want to copy permission to
8 item.BreakRoleInheritance(true); //you must set this to true or it wouldn’t work
9 while (item.RoleAssignments.Count > 0)
10 {
11 item.RoleAssignments.Remove(0);
12 }
13 foreach (SPRoleAssignment role in roles)
14 {
15 item.RoleAssignments.Add(role);
16 }
17 base.DisableEventFiring();
18 item.Update();
19 base.EnableEventFiring();
20 }
21 }

2 thoughts on “How to add or remove permissions to a list item in sharepoint

  1. that’s exactly the same code i ran but sharepoint, like the piece of sharepoint that it is, threw the excruciatingly helpful “value does not fall within the expected range.” error that it loves so much. is there any chance you could please repost your code but this time taking into account what to do if your sharepoint farm is set up in the most frustrating way possible, that being different application pools with different identities (and i’m guessing bashed in with a hammer and sneezed on by an ebola monkey because nothing else could be so difficult to work with)?

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>