.NET King Logo Comments on: Setting Item Level Security programmatically for SharePoint list items
Skip Navigation Links
Home
Weblog
How to... (video)
Articles
About us
Contact us
0 Items in Video cart!

Setting Item Level Security programmatically for SharePoint list items (4/15/2008)

 This is not a unique post. You may find this post in many weblogs and websites, but I am putting this code here for my own reference. In this code I break the security inheritance for a list item and provide contribute permission for a group that has been already created in SharePoint site.

 

            SPListItem myItem = properties.ListItem;
//This line detaches the item security from the list
            myItem.BreakRoleInheritance(false);
            myItem.Update();
            SPWeb myWeb = properties.OpenWeb();
// Creates an object of type SPMember to represent a group
            SPMember member = myWeb.SiteGroups["TestGroup"];
// We can simply cast a SPMember object into a SPPrincipal variable
            SPPrincipal principal = (SPPrincipal)member;
// A role definition is required to represent a permission level in SharePoint site
            SPRoleDefinition roledefinition =
                myWeb.RoleDefinitions.GetByType(SPRoleType.Contributor );
            SPRoleAssignment myRoleAssignment = new SPRoleAssignment(principal);
            myRoleAssignment.RoleDefinitionBindings.Add(roledefinition);
            myItem.RoleAssignments.Add(myRoleAssignment);
            myItem.Update();

 Enjoy the code.
Alireza


By: saritha  
Hi Alireza - Thanks for the code am looking for similar thing.I want to set permissions for user and his groups.I mean if he is more than one group can you give me that code please.
URL:  
Your name:
Email:
URL:
Comments:
 

 -