SharePoint Event Receivers

Ingrid P. Williams 1 Reputation point
2021-03-22T14:31:01.397+00:00

I am trying to find out How to get distinct usergroup field values from another SharePoint list then comparing each value returned to the current list column values(allows Multiples) using an event receiver then using those values to get all members in that group. THe value returned will be a single value, where the existing list item has multiple values. I have never created an event receiver but I am unable to accomplish this in a workflow. Please help if possible

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,675 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jerryzy 10,566 Reputation points
    2021-03-23T02:59:57.523+00:00

    Hi @Ingrid P. Williams ,

    You can get a SPGroup object for the usergroup field and then get all the distinct user members using SPGroup.Users, then compare using the multiple person field, here is a sample code for your reference:

    Create a static extension method in a static class like below:

    using Microsoft.SharePoint;  
    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Threading.Tasks;  
      
    namespace SharePointProject22  
    {  
        public static class SPUserExtension  
        {  
            public static bool UserExist(this SPGroup grp, SPUser usr)  
            {  
                foreach (SPUser str in grp.Users)  
                {  
                    if (str.LoginName == usr.LoginName)  
                    {  
                        return true;  
                    }  
                }  
                return false;  
            }  
        }  
    }  
    

    Back to EventReceiver, get the usergroup field from another list and get the current list column values (allow Multiple value) for comparing:

    using System;  
    using Microsoft.SharePoint;  
      
    namespace SharePointProject22.TestMulUserValue  
    {  
        /// <summary>  
        /// List Item Events  
        /// </summary>  
        public  class TestMulUserValue : SPItemEventReceiver  
        {  
            /// <summary>  
            /// An item was added.  
            /// </summary>  
            public override void ItemAdded(SPItemEventProperties properties)  
            {  
                base.ItemAdded(properties);  
      
                SPWeb web = properties.Site.OpenWeb();  
                SPList list = web.Lists["List1"];  
                SPListItem item = list.GetItemById(1);  
                var groupName = item["usergroup"].ToString().Split('#')[1];  
                SPGroup group = web.Groups.GetByName(groupName);  
                SPFieldUserValueCollection users= (SPFieldUserValueCollection)properties.ListItem["MulUser"];  
                foreach (SPFieldUserValue user in users)  
                {  
                   Boolean IsExisted = SPUserExtension.UserExist(group, user.User);  
                }  
                 
            }  
      
             
      
      
        }  
    }  
    

    item["usergroup"] is getting the usergroup field (the field name is "usergroup", need to change the real one in your side), properties.ListItem["MulUser"] is getting the multiple value person field (the field name is "MulUser", also need to change with your own value).

    Thanks
    Best Regards


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.