Summary : How to Create SpsecurityEventReceiver in SharePoint 2013,Create a GroupUserAdded() SPSecurityEventReceiver EventReceiver in SharePoint 2013,sharepoint 2013 event receivers,SharePoint 2013,SPSecurityEventReceiver in SharePoint 2013,GroupUserAdded() Event Receiver
Earlier in August 2012 we discussed about the new Event Receivers for Groups,Users,Roles and Inheritance Introduced in SharePoint 2013.
In this post we will look at a Step-by-Step example of how to create a GroupUserAdded() Event Receiver using the new SPSecurityEventReceiver class.As the name suggests GroupUserAdded() method is used to handle events after a new user is added to a group.For example, after a new user is added to a group, the user must be added to a bunch of other groups or to different lists or libraries with a particular set of permissions. This can be very useful to admins as they can setup this receiver to send notifications of any new user added to the groups with the permissions.
As if now there is no template available in Visual studio 2012 to add any kind of SPSecurityEventReceiver class Event Receivers. So to create a GroupUserAdded() Event Receiver, we need to register it first using the Feature Receiver file.Lets look at what we need to begin.
1. Feature Receiver – A Feature Receiver is something that runs when a feature is Installed,Activate,Deactivated Uninstalled and Upgraded.In our case however, we will Create a new Feature Receiver that will be responsible to register a new GroupUserAdded() Event Receiver when the parent feature is Activated on the site. In this Part 1 post we will look at the steps to create the required Solution and the Feature Receiver.
2. Event Receiver – Once the Feature Receiver is added we will add a new GroupUserAdded() Event Receiver class to our project that will contain the code to handle user added events on the site.For this example, the GroupUserAdded() code verifies if the user is added to a group “Team Site Members” and if it does the user gets added to another group “Added Members” that exists in the same site.
“SPSecurityEventReceiver” class was introduced to help developers add Event Handlers around SharePoint Groups, Users, Roles and Permission Inheritance. Details on What kind of events can be handled can be see here
Lets look at the Steps –
Step 1 – Create an Empty SharePoint 2013 Project.
Step 2 – Enter the SharePoint site URL for debugging and select Deploy it as a Farm Solution.
Step 3 – Next Add a new Feature (right click Feature and click Add Feature).
Step 4 – Next we will add a new Feature Receiver where all the Code to register the new SPSecurityEventReceiver GroupUserAdded() EventReceiver will be added.When this new Feature activates, the Feature Receiver runs and registers the GroupUserAdded() event.To add a new Feature Receiver right click Feature 1-> Add Event Receiver (see below).
Step 5 – Next add the following in FeatureActivated() method to register the event.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
SPWeb web = properties.Feature.Parent as SPWeb;
//Adding the GroupUserAdded event
SPEventReceiverDefinition grpUserAdded = web.EventReceivers.Add();
grpUserAdded.Name = "Event Receiver GroupUserAdded";
grpUserAdded.Type = SPEventReceiverType.GroupUserAdded;
grpUserAdded.Assembly = Assembly.GetExecutingAssembly().FullName;
grpUserAdded.Class = "GroupUserAddedEventReceiver.UserAddedEventReceiver";
Your Feature should look something like below
So now you’re all set with the Feature Receiver and the code to register GroupUserAdded() EventReceiver to your site. Next we will look at how to create a GroupUserAdded() EventReceiver class and how to handle User added events when a user is added to a particular group in the site. Part 2 >>