• Call Toll Free: 1-855-909-3300

How to Create a GroupUserAdded() SPSecurityEventReceiver in SharePoint 2013

when a triggering action like adding, deleting, updating, moving, checking in and checking out occurs on any specified object of SharePoint, an event receiver is called up. Sharepoint has introduced a new event receiver known class known as SPSecurityEventReceiver.

Now, find out how you can create a group user added event receiver through this. We will do this by creating a custom list called “group details” whenever a user joins the group.

Add a new Class file “UserAddedEventReceiver.cs” to your project.

Step 7 – Make sure that your class inherits SPSecurityEventReceiver class. Add the following Code for GroupUserAdded() in your UserAddedEventReceiver class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;

namespace GroupUserAddedEventReceiver
{
class UserAddedEventReceiver : SPSecurityEventReceiver
{
public override void GroupUserAdded(SPSecurityEventProperties properties)
{
base.GroupUserAdded(properties);

//Get the user being added
SPUser addedUser = properties.Web.AllUsers.GetByID(properties.GroupUserId);

SPSecurity.RunWithElevatedPrivileges(delegate()
{
if (addedUser != null && properties.GroupId == 8)
{
SPGroup spGroup = properties.Web.Groups[“Added Members”];

if (spGroup != null)
spGroup.AddUser(addedUser);
}
});
}

}
}

The code should look something like below.

Step 8 – Once the code is added Start and debug the project.The project will add the solution in your site and activate the feature as well. Feature activation will kick off the Feature Receiver and registers the GroupUserAdded() events.

Step 9 – Once the solution is deployed verify if the Feature is activated.To do this navigate to your SharePoint site -> Settings -> Manage site Features. In the feature list look for “GroupUserAddedEventReceiver Feature 1″ feature.

Step 10 – Now Navigate to the People and groups -> Team Site Members to add a new user. Follow the screens below –

Select New -> “Add Users to this group”.

Leave a Reply

Techarex NetWorks Products