Setting List Item Permissions Programatically in sharepoint

I am setting permissions on a individual SPListItem.  I’ll create a Role Assignment (which essentially represents the user), bind a Role Definition to the Assignment (such as “Full Control”, etc.), then add the Role Assignment to the object’s RoleAssignments collection.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
namespace Avinash
{
    class Program
    {
        static void Main(string[] args)
        {
            SetListItemPermission();
        }
        static void SetListItemPermission ()
        {
            //Get SPWeb object
            SPSite Site = new SPSite("http://<url>");
            SPWeb Web = Site.OpenWeb();
 
            //Get Role Definition
            SPRoleDefinition RoleDefinition = Web.RoleDefinitions.GetByType(SPRoleType.Administrator);
 
            //Get SPListItem
            SPList List = Web.Lists["<list name>"]; // Or you can use Web.GetList method for better performance
            SPListItem ListItem = List.Items[1];
            //Create new Role Assignment
            SPRoleAssignment RoleAssignment = new SPRoleAssignment("<login name>",
                                                                    "<email address>",
                                                                    "<display name>",
                                                                    "");
            RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);
            //Break permission if necessary
            if(!ListItem.HasUniqueRoleAssignments)
            {
                        ListItem.BreakRoleInheritance(true);
            }
            //Add Role Assignment to SPListItem's Role Assignment Collection
            ListItem.RoleAssignments.Add(RoleAssignment);
            ListItem.Update();
        }
 
    }
}

Thanks!
Avinash

calendarMarch 13, 2012 · cardInfoyen · commentsNo Comments
tagTags: , , ,  · Posted in: List, MOSS, SharePoint

Leave a Reply

Spam Protection: , required

myworldmaps infoyen