0

I have designed a timer job to daily check for events and send a reminder email to registered users 1 day prior to event occurrence.

The following code fetches all event registrations for the given date. But need to fetch user email id from that foreach loop to send email. The list does not have a column for user email id, but , has 1 for user display name as :

item["Person Name"]

The following timer job code fetches events:

                        using (SPSite site = new SPSite(siteUrl))
                        {
                            using (SPWeb web = site.OpenWeb())
                            {
                                SPList sPList = web.Lists.TryGetList(listName);
                                SPQuery sPQuery = new SPQuery();
                                sPQuery.Query="<Where><Eq><FieldRef Name=\"Start_x0020_Time\" /><Value IncludeTimeValue='FALSE' Type=\"DateTime\"><Today OffsetDays=\"1\"/></Value></Eq></Where>";
                            SPListItemCollection items = sPList.GetItems(sPQuery);
                                if (items.Count > 0)
                                {
                                foreach (SPListItem item in items)
                                {
    //need to call email sending function here. Has item["Person Name"] column in the list but not 

//email. Is it possible to get email without adding email column to list?
                                }
                                }
    
                            }
                        }
                               }

Can u help me out to get the user email email id inside the loop to send email?

1 Answer 1

1

Yes, it's possible. You below is the code you should try

            using (SPSite site = new SPSite(siteUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList sPList = web.Lists.TryGetList(listName);
                    SPQuery sPQuery = new SPQuery();
                    sPQuery.Query = "<Where><Eq><FieldRef Name=\"Start_x0020_Time\" /><Value IncludeTimeValue='FALSE' Type=\"DateTime\"><Today OffsetDays=\"1\"/></Value></Eq></Where>";
                    SPListItemCollection items = sPList.GetItems(sPQuery);
                    if (items.Count > 0)
                    {
                        foreach (SPListItem item in items)
                        {
                            List<string> emails = new List<string>();
                            SPFieldUserValueCollection sPFieldUserValues = item["Person_x0020_Name"] as SPFieldUserValueCollection;
                            foreach (var sPFieldUserValue in sPFieldUserValues)
                            {
                                emails.Add(sPFieldUserValue.User.Email);
                            }
                            // Now emails string collection contains emails of all the users from Person Name column
                        }
                    }

                }
            }

Important: I have assumed that "Person_x0020_Name" would be your internal name of the column

Reference Link: https://csharp.hotexamples.com/examples/-/SPFieldUserValueCollection/-/php-spfielduservaluecollection-class-examples.html

2
  • SPFieldUserValueCollection sPFieldUserValues = item["Person_x0020_Name"] as SPFieldUserValueCollection; is returning null
    – user105078
    Commented Sep 7, 2022 at 16:55
  • Hi @Kalpesh, Thanks for the reference link you shared. Fixed it with another code sample in it.
    – user105078
    Commented Sep 7, 2022 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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