SharePoint 2010: Setting Item Level Permission on a List Folder
Scenario:
Sometime we run into a situation where we got to set a item level permission on a particular folder in a list. Possible reason to that can be hiding data from other users, if the folder is owned by a specific user and contain some private information.
About the Code:
In the following, I had a similar, aforementioned, requirement with a slight punch of a management group that was suppose to view everything and then I had normal users, among whom, the owner of the folder was suppose to have rights to viewing and contributing. Thus I had to set permissions for the users who owned the folder and then for the management group. BTW originally it was a sand-boxed solution.
private void setPermissions(SPFolder folder) { SPGroupCollection spc = web.SiteGroups; //Break the role inheritance in order to assign individual rights on folders if (!folder.Item.HasUniqueRoleAssignments) { folder.Item.BreakRoleInheritance(true); } //This was quite tricky. I first delete all the user/groups and then reassigned my //group and the user. In high likelihood, one would bump into an exception if one //tries to delete the system account.So I've this Try&Catch block. while (folder.Item.RoleAssignments.Count > 0) { try { folder.Item.RoleAssignments.Remove(0); } catch (Exception) { break; } } //Role Assignment For the Current User SPUser CurrentUser = SPContext.Current.Web.CurrentUser; SPGroup group = spc["GroupName"]; SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal) CurrentUser); roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Administrator)); folder.Item.RoleAssignments.Add(roleAssignment); //Role Assignment for the Group "Contentteam - Management" roleAssignment = new SPRoleAssignment((SPPrincipal)group); roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Administrator)); folder.Item.RoleAssignments.Add(roleAssignment); //Set AllowUnsafeUpdates to true, even if you've done this already. This //weird site.AllowUnsafeUpdates = true; folder.Item.Update(); }
Verification "The Litmus Test":
Now to check the effects of the code, go to the list where your folder resides. Then look in the "List" tab in the ribbon. Press "List Permissions" and then you will come across the following screen. You will see a notification on the top of the page. Press "Show me uniquely secured items of this list"

and then a dialogue box pops up. Select the "manage permissions" link against the folder you worked on and you will see all the users and group who have the rights to access this folder. you will notice that, only those users and groups are present which you added.
So this did the trick for me. If you didn't understand the code then you got understand the role and right configuration in SharePoint 2010 first and obviously you can ask me. BTW I'm open to suggestions.
References:
http://www.thespgeek.com/2011/08/assigning-sharepoint-list-item-level.html (Great work SP Geek)
Comments
Post a Comment