Tuesday, February 21, 2012

Setting customised Item Level Permission to the Group


How to set Customized item level Permissions to SharePoint Groups through Code C# in event receiver?
Here in Example we will assign “View Only” Permission to “HomeOwner”Group.

Note : Here “View Only” is Customized Permission created with Specific rights assigned in that group and “HomeOwner” is group Created in share point Group.

Assuming that you know how to create the Event Receiver in SharePoint and its Events.
Example. We will use C# code in Event receiver.

Following is the function written to set the Group Level Permission.
private void SetPermissionToGroup(stringgroupName, SPListItem listItem, ArrayList permissionList, SPWebspWeb)
{

SPGroup spGroup = spWeb.SiteGroups[groupName];
foreach (stringpermission in permissionList)
{
SPRoleDefinition spRole = spWeb.RoleDefinitions[permission];
SPRoleAssignment roleAssignment = new SPRoleAssignment(spGroup);
roleAssignment.RoleDefinitionBindings.Add(spRole);
if (!listItem.HasUniqueRoleAssignments)
{

listItem.BreakRoleInheritance(true);
}

listItem.RoleAssignments.Add(roleAssignment);
}
}


In the above Function SetPermissionToGroup we require 4 paramenters

Parameter 1: groupName: Which requires the Group name to passed to set the Permission for e.g “HomeOwner”

Parameter 2: SPListItemParamete this: Which requires a SPListItem for which the permission is to be set.

Parameter 3: ArrayList : Which requires the list of Permission to be set on the Item.For eg. new ArrayList { "View Only" }

Parameter 4: SPWeb : Which requires the SPWeb.

You can pass the Parameter as given below in your Event. I have used in ItemAdded Event.

SetPermissionToGroup("Homeowners", item, new ArrayList { "View Only" }, Site);

No comments:

Post a Comment