Create Group Event Receivers in SharePoint 2013

Leave a Comment
Hi All,

IN the following will demonstrate how to create group user event receivers in share point 2013.

--> Create SharePoint Empty Project 2013.
--> Add Feature to the Project(in our case , SampleGroupUserEventReceiver Feature)
--> Add Event Receiver to the Feature.(GroupEventReceivers).

Now Create one custom class called as  "GroupUserAddEvent "

   class GroupUserAddEvent : SPSecurityEventReceiver
    {
        public override void GroupUserAdded(SPSecurityEventProperties properties)
        {
            base.GroupUserAdded(properties);

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

            // Get the group details to which the user is added
            SPGroup groupAdded = properties.Web.Groups.GetByID(properties.GroupId);

            // Get the list instance
            SPList oList= properties.Web.Lists.TryGetList("Groups");
            if (oList!= null)
            {
                // Add a new item
                SPListItem oListItem= list.Items.Add();
                // update the title column
                oListItem["Title"] =  userAdded.Name ;
                oListItem["GroupName"]=groupAdded.
                // Update the item
                oListItem.Update();
                // Update the list
                oList.Update();
            }
        }
    }

In Feature Activation , write the below code

[Guid("cc74df3c-7a11-4ae2-807c-ac0be794e692")]
    public class GroupEventReceiversEventReceiver : SPFeatureReceiver
    {
        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;
            //Feature Name and EventReceiver Class.
            grpUserAdded.Class = "SampleGroupUserEventReceiver.GroupUserAddEvent";
            grpUserAdded.Update();
            web.Update();
        }
}

Now build your project and deploy..experience the new event receivers....

Related Post

0 comments:

Post a Comment