using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using System.Linq;
namespace EricSunSharePointProject.Features.CustomUserGroupFeature
{
/// <summary>
/// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
/// </summary>
/// <remarks>
/// The GUID attached to this class may be used during packaging and should not be modified.
/// </remarks>
[Guid("7ae2e739-1863-4b34-b3cb-a7fd6fd04fa4")]
public class CustomUserGroupFeatureEventReceiver : SPFeatureReceiver
{
// Uncomment the method below to handle the event raised after a feature has been activated.
//public override void FeatureActivated(SPFeatureReceiverProperties properties)
//{
//}
// Uncomment the method below to handle the event raised before a feature is deactivated.
//public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
//{
//}
// Uncomment the method below to handle the event raised after a feature has been installed.
//public override void FeatureInstalled(SPFeatureReceiverProperties properties)
//{
//}
// Uncomment the method below to handle the event raised before a feature is uninstalled.
//public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
//{
//}
// Uncomment the method below to handle the event raised when a feature is upgrading.
//public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
//{
//}
const string Administrators = "EricSun Content Administrators";
const string Approvers = "EricSun Content Approvers";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
string groupDescription = "EricSun Content";
try
{
using (SPWeb web = properties.Feature.Parent as SPWeb)
{
CreateSubSiteGroup(web, Administrators, GetAdministratorPermission(), groupDescription + " Administrators Group", Administrators, "Can view, add, update, delete, and customize list items and documents.");
CreateSubSiteGroup(web, Approvers, GetApproverPermission(), groupDescription + " Approvers Group", Approvers, "Can view, and approve list items and documents.");
}
}
catch (SPException ex)
{
}
}
// Uncomment the method below to handle the event raised before a feature is deactivated.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
using (SPWeb web = properties.Feature.Parent as SPWeb)
{
DeleteSubSiteGroup(web, Administrators);
DeleteSubSiteGroup(web, Approvers);
}
}
catch (SPException ex)
{
}
}
/// <SUMMARY>
/// Create group
/// </SUMMARY>
private void CreateSubSiteGroup(SPWeb web, string groupName, SPBasePermissions PermissionLevel, string groupDescription, string roleName, string description)
{
try
{
SPUserCollection users = web.AllUsers;
SPUser owner = web.SiteAdministrators[0];
SPMember member = web.SiteAdministrators[0];
SPGroupCollection groups = web.SiteGroups;
if (!groups.Cast<SPGroup>().Any(g => g.Name.Equals(groupName, StringComparison.Ordinal)))
{
//add new group if not found
groups.Add(groupName, member, owner, groupDescription);
}
SPGroup newSPGroup = groups[groupName];
SPRoleDefinition role = new SPRoleDefinition();
role.Name = roleName;
role.Description = description;
role.BasePermissions = PermissionLevel;
if (!web.RoleDefinitions.Cast<SPRoleDefinition>().Any(r => r.Name.Equals(roleName, StringComparison.Ordinal)))
{
//add role definition if not found
web.RoleDefinitions.Add(role);
}
role = web.RoleDefinitions[roleName];
SPRoleAssignment roleAssignment = new SPRoleAssignment(newSPGroup);
roleAssignment.RoleDefinitionBindings.Add(role);
web.RoleAssignments.Add(roleAssignment);
web.Update();
}
catch (SPException ex)
{
}
}
/// <SUMMARY>
/// Delete group for subsite
/// </SUMMARY>
private void DeleteSubSiteGroup(SPWeb web, string groupName)
{
try
{
SPGroupCollection groups = web.SiteGroups;
groups.Remove(groupName);
web.Update();
}
catch (SPException ex)
{
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private SPBasePermissions GetAdministratorPermission()
{
return SPBasePermissions.EditListItems | SPBasePermissions.ViewListItems | SPBasePermissions.DeleteListItems
| SPBasePermissions.AddListItems | SPBasePermissions.OpenItems;
}
private SPBasePermissions GetApproverPermission()
{
return SPBasePermissions.ApproveItems;
}
}
}