{sharepoint} Setting List Item Permissions Programatically in sharepoint

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();
        }
 
    }
}
posted @ 2016-05-17 21:32  Areas  阅读(281)  评论(0编辑  收藏  举报