/// <summary>
/// Add Permission Level to a Sharepoint user group collection
/// </summary>
/// <param name="spWeb">The sp web.</param>
/// <param name="permissionName">Name of the permission.</param>
/// <param name="groupCollection">The group collection.</param>
public static void AddPermissionsToGroupCollection(SPWeb spWeb, string permissionName, string[] groupCollection)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
try
{
//Allow updating of some sharepoint lists, (here spUsers, spGroups etc...)
spWeb.AllowUnsafeUpdates = true;
SPRoleDefinition roleDefinition = spWeb.RoleDefinitions[permissionName];
foreach (string group in groupCollection)
{
SPGroup spGroup = spWeb.Groups[group];
SPRoleAssignment roleAssignment = new SPRoleAssignment(spGroup);
SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
roleDefBindings.Add(roleDefinition);
spWeb.RoleAssignments.Add(roleAssignment);
roleDefinition.Update();
}
}
catch (Exception ex)
{
//Error handling logic should go here
throw ex;
}
finally
{
spWeb.AllowUnsafeUpdates = false;
}
});
}
Ok this is it.
Well... I called above method from "FeatureActivated" event of a feature. For "FeatureDeactivating" event I tried to achieve the contrary to this, delete a permission level from a user group. But didn't succeed. :(
If you have any idea pls let us know.Thanks in advance.