question

IngridPWilliams-1035 avatar image
0 Votes"
IngridPWilliams-1035 asked Jerryzy commented

SharePoint Event Receivers

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-dev
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @IngridPWilliams-1035 ,

Is there any update ? If the post is helpful, I suggest you can accept as answer so that it could also help others who have the similiar question in the forum.

Thanks

0 Votes 0 ·

1 Answer

Jerryzy avatar image
0 Votes"
Jerryzy answered Jerryzy commented

Hi @IngridPWilliams-1035 ,

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Jerryzy-MSFT

Would it matter that I am using ItemUpdating instead of ItemAdded

0 Votes 0 ·
Jerryzy avatar image Jerryzy IngridPWilliams-1035 ·

ItemUpdating won't get the latest updated value, if you want to compare the updated value, use ItemUpdated instead.

0 Votes 0 ·