1: #region SetSecurityForItem
2: // This function sets the Item Level Security for the document
3: private void SetSecurityForItem(SPItemEventProperties properties, SPWeb spWebSet, string methodType)
4: {
5: SPFieldUserValueCollection sharePointGroups;
6: SPList spList = properties.ListItem.ParentList;
7: SPListItem docItem = properties.ListItem;
8:
9: bool isChangeSecurity = (bool)docItem["ChangeSecurity"];
10:
11: if (isChangeSecurity)
12: {
13: sharePointGroups = group2;
14: }
15: else
16: {
17: sharePointGroups = group1;
18: }
19: //Break the Inheritance to assign new role definition to the group
20: docItem.BreakRoleInheritance(false);
21:
22: //Remove the previous RoleAssignment if this is an update
23: if (methodType == "ItemUpdated")
24: {
25: //remove all the inherited roles
26: for (int i = docItem.RoleAssignments.Count - 1; i >= 0; --i)
27: {
28: docItem.RoleAssignments.Remove(i);
29: }
30: }
31: // Get Role Definitions
32: SPRoleDefinitionCollection roleDefinitions = HttpRuntime.Cache.Get("WebRoleDefinitions") as SPRoleDefinitionCollection;
33: if (roleDefinitions == null)
34: {
35: roleDefinitions = spWebSet.RoleDefinitions;
36: HttpRuntime.Cache.Insert("WebRoleDefinitions", roleDefinitions);
37: }
38:
39: SPRoleDefinition[] rolesToApply = new SPRoleDefinition[1] { roleDefinitions[roleType] };
40:
41: foreach (SPFieldUserValue fuv in sharePointGroups)
42: {
43: addPermissionToListItem(properties.ListItem.Web, properties.ListItem, fuv, rolesToApply);
44: }
45:
46: }
47: #endregion