1

I have a scenario where I have to develop multiple event receivers, on which I want your help.

the scenario is :

I have a list say "userlist" with two columns as "Title" and "username" and a SharePoint User Group say "Project Visitor".

username field is of type "People or Groups"

now what i want is, if anyone enter a record in the userlist then the name selected in username should be added to the Project Visitors group, in the same way, if that particular item is updated or deleted in the list then the change should also reflect in the SharePoint group.

the whole thing should also work in reverse as well, means if I add, update or delete any member in the group then the change should reflect in the list as well

how can i do that ?

1

You can simply create the Event receiver as explained in example here : How to create a custom list level event receiver in SharePoint 2013 and SharePoint 2010

Or the other example you can use here: Creating a simple Event Receiver in SharePoint 2013.

Once you create the event receiver, you can place code of adding current user to group, see this link here: Add a user programmatically to a User Group in SharePoint.

You can simply manage operation as per the list item event, whether userlist item is being deleted or updated, there are separate event receiver methods available.

You can also remove the current user from group. There are some methods explained here you see for reference here: Get, Add or Remove SharePoint groups and users programmatically.

For the reverse functionality, you can attach users and group level event receiver explained here: Create GroupUserAdded Event Receiver in SharePoint 2013, List of Event receiver in SharePoint 2013 for Groups, Users etc: SharePoint 2013 – New Event Receiver for Groups,Users,Roles,Inheritance

1

You need to two event handlers

  1. Attached to the List

    You need to ItemUpdated and ItemAdded events

  2. Attached to SharePoint User Events - SPSecurityEventReceiver class was introduced to help developers add Event Handlers around SharePoint Groups, Users, Roles and Permission Inheritance.

    You can create this handler in a Feature Receiver

    SPEventReceiverDefinition grpUserAdded = web.EventReceivers.Add();
    grpUserAdded.Name = "Event Receiver GroupUserAdded";
    grpUserAdded.Type = SPEventReceiverType.GroupUserAdded;
    grpUserAdded.Assembly = Assembly.GetExecutingAssembly().FullName;
    grpUserAdded.Class = "GroupUserAddedEventReceiver.UserAddedEventReceiver";
    grpUserAdded.Update();
    
    web.Update();
    
    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);
                }
            });
        }           
    }
    

    http://www.learningsharepoint.com/2012/12/27/create-a-groupuseradded-spsecurityeventreceiver-eventreceiver-in-sharepoint-2013-part-2/

2
  • According to the OP's requirements, he will also need ItemDeleting on the list to remove the user from the group if the list item is deleted. Apr 30 '15 at 16:21
  • @DylanCristy Yes. I was just pointing him in the right Direction. :) Apr 30 '15 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.