Create GroupAdded Event Receiver in SharePoint 2013

In this article you will see how to create a GroupAdded event receiver in SharePoint 2013.

Introduction

An event receiver is a custom code that is called when a triggering action such as adding, updating, deleting, moving, checking in, and checking out occurs on a specified SharePoint object such as site collections, sites, lists, and workflows. SharePoint 2013 introduced a new event receiver class called "SPSecurityEventReceiver". The custom event receiver class must derive from "SPSecurityEventReceiver" (Please refer to: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurityeventreceiver.roleassignmentadded.aspx). In this article we will see how to create group added event receiver in SharePoint 2013. I have created a custom list called "Group Details". When a new group is added to the site a new item will be created in "Group Details" that will specify the group name, user login name and created date and time.

Create Empty SharePoint Project

  1. Open Visual Studio 2012 (Run as administrator).
  2. Go to "File" => "New" => "Project...".
  3. Select "SharePoint 2013 Project" template from the installed templates.
  4. Enter the name "GroupAddedEventReceiver" and then click on Ok.

    GroupAddedEventReceiver.jpg

    Figure 1: New Project template
     
  5. Select the site and security level for debugging.

    Specify-the-site-and-security-level-for-debugging.jpg

    Figure 2: Specify the site and security level for debugging

  6. Click on Finish.

Add Feature to the project

  1. In the Solution Explorer, right-click on the Features folder.
  2. Click on "Add Feature".
  3. Rename Feature1 to "GroupAddedFeature".
  4. Double-click on GroupAddedFeature and specify the Title & Description.

GroupAddedFeature.jpg

Figure 3: GroupAddedFeature

Add Event Receiver to the Feature

Feature Events receivers are methods that execute when one of the following feature-related events occur in SharePoint:

  • A feature is installed
  • A feature is activated
  • A feature is deactivated
  • A feature is removed

Please refer to http://msdn.microsoft.com/en-us/library/ee231604.aspx to Add Feature Event Receivers. When a feature is activated please use members of the SPEventReceiverDefinition class to register a GroupAdded event receiver.

  1. In the Solution Explorer, right-click on GroupAddedFeature.
  2. Click on Add Event Receiver.
  3. Double-click on "GroupAddedFeature.EventReceiver.cs".
  4. Replace the code with the following:

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using System.Reflection;

namespace GroupAddedEventReceiver.Features.GroupAddedFeature
{
   
/// <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("624a8f45-4aa5-402f-9f05-4c41ce3a6215")]
   
public class GroupAddedFeatureEventReceiver : SPFeatureReceiver
    {
       
public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = properties.Feature.Parent
as SPWeb;

            //Adding the GroupAdded event
            SPEventReceiverDefinition grpUserAdded = web.EventReceivers.Add();
            grpUserAdded.Name =
"Event Receiver GroupAdded";
            grpUserAdded.Type = SPEventReceiverType.GroupAdded;
            grpUserAdded.Assembly = Assembly.GetExecutingAssembly().FullName;
            grpUserAdded.Class =
"GroupAddedEventReceiver.GroupAddedEvent";
            grpUserAdded.Update();
            web.Update();
        }
    }
}


Add a custom event receiver class

  1. In the Solution Explorer, right-click on the project and click on "Add".
  2. Click on "New Item".
  3. Select the Class template from the installed templates.
  4. Enter the name and click on "Add".

    Add-new-item.jpg

    Figure 4: Add new item
     
  5. Replace GroupAddedEvent.cs with the following code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.SharePoint;

    namespace GroupAddedEventReceiver
    {
       
    class GroupAddedEvent:SPSecurityEventReceiver
        {
           
    publicoverridevoid GroupAdded(SPSecurityEventProperties properties)
            {
               
    base.GroupAdded(properties);
               
    // get the custom list "Group Details"
                SPList list = properties.Web.Lists.TryGetList("Group Details");

                // Check if the list exists
                if (list != null)
                {
                   
    // Add a new item
                    SPListItem item = list.Items.Add();
                   
    // Update the title column with group name, date time and user display name
                    item["Title"] = "Group name: " + properties.GroupName + " is created on " + DateTime.Now + " by " + properties.UserDisplayName;
                   
    // Update the item
                    item.Update();
                   
    // Update the list
                    list.Update();
                }
            }
        }
    }

  6. Solution Explorer looks like the following:

Solution-Explorer.jpg

Figure 5: Solution Explorer

Deploy and test the solution

  1. Right-click on the solution and click on "Deploy".

  2. Navigate to the SharePoint site.

  3. Click on "Site Settings".

    Site-Settings.jpg

    Figure 6: Site Settings

  4. Click on "People and groups" that is available under the "Users and Permissions" section.

    People-and-groups.jpg

    Figure 7: People and groups

  5. Click on "Groups" in the Quick Launch Bar.

  6. Click on "New".

    Create-new-group.jpg

    Figure 8: Create new group

  7. Enter the group name and specify the permission level.

    Create-group.jpg

    Figure 9: Create group

  8. Click on the "Create" button.

  9. A new group is created successfully.

  10. Click on "Group Details" in the Quick Launch Bar.

  11. A new item is created as shown in Figure 10.

    A-new-item-created-in-Group-Details-list.jpg


    Figure 10: A new item was created in the Group Details list

Summary

Thus in this article you have seen how to create a group added event receiver in SharePoint 2013.
 

Up Next
    Ebook Download
    View all
    Learn
    View all