Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Prevent duplicate user in Email

(0) ShareShare
ReportReport
Posted on by 121

Hi, i have requirement to create email in CRM using C#. 

The following is a piece of code used to get the list of recipients.

for (int iusr = 0; iusr < dtableUserQuality.Rows.Count; iusr  )
{
systemuserid = Guid.Empty;
systemuserid = new Guid(dtableUserQuality.Rows[iusr][0].ToString());
QueryExpression to_query = new QueryExpression("systemuser");
to_query.Criteria = new FilterExpression();
to_query.Criteria.AddCondition("systemuserid", ConditionOperator.Equal, systemuserid);
to_query.ColumnSet = new ColumnSet(true);

EntityCollection to_colect_email = _service.RetrieveMultiple(to_query);


toUserParty.EntityName = "systemuser";

foreach (Entity to_ent_email in to_colect_email.Entities)
{
string fullname = to_ent_email.GetAttributeValue("fullname");



Entity fromParty = new Entity("activityparty");
EntityReference userParty = new EntityReference("systemuser", to_ent_email.Id);

fromParty.Attributes.Add("partyid", userParty);

toUserParty.Entities.Add(fromParty);

}
}

if (toUserParty.Entities.Count > 0)
{
email.Attributes.Add("to", toUserParty);
}

The email created succesfully, however the code allowed duplicate user exist in the recepients list. 

1213131.png

how to prevent users already in the recipient list from being duplicated.

Thanks

  • FikriHailal Profile Picture
    121 on at
    RE: Prevent duplicate user in Email

    Hi david, using your second idea, finally i can prevent duplicate recepient. Thanks

  • gdas Profile Picture
    50,091 Moderator on at
    RE: Prevent duplicate user in Email

    Its seems  dtableUserQuality. table has duplicate entry.

  • FikriHailal Profile Picture
    121 on at
    RE: Prevent duplicate user in Email

    Hi Gouthan, i found that after using your code the results didn't change.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Prevent duplicate user in Email

    Hi ,

    Try with this  - 

       for (int iusr = 0; iusr < dtableUserQuality.Rows.Count; iusr  )
                {
                    systemuserid = Guid.Empty;
                    systemuserid = new Guid(dtableUserQuality.Rows[iusr][0].ToString()); 
                    Entity to_colect_email = _service.Retrieve("systemuser", systemuserid, new ColumnSet(true));   //Use retrieve as you are using Guid in the filter operation , and put column name which you need 
    
    
                    toUserParty.EntityName = "systemuser";
                    string fullname = to_colect_email.GetAttributeValue("fullname");  // I dont see any reason for this code 
    
                    Entity fromParty = new Entity("activityparty");
                    EntityReference userParty = new EntityReference("systemuser", to_colect_email.Id);
                    fromParty.Attributes.Add("partyid", userParty);
                    toUserParty.Entities.Add(fromParty);             
                }
    
                if (toUserParty.Entities.Count > 0)
                {
                    email.Attributes.Add("to", toUserParty);
                }

  • Anthony Dong Profile Picture
    on at
    RE: Prevent duplicate user in Email

    Some sample code for your reference at docs.microsoft.com/.../hh210213(v=crm.8)

  • Verified answer
    David Jennaway Profile Picture
    14,065 on at
    RE: Prevent duplicate user in Email

    There are 2 ways you could do this:

    1. If the query that you use to get the list of users can be written as one query on the systemuser entity that can be executed just once, then you can set Distinct = true, so that it will only return each record once. You currently aren't doing this, as you're submitting the query within a loop
    2. Or, before you add the recipient to the toUserParty.Entities collection, check if it's already been added. I normally create a separate collection (of type List<Guid> ) to track the Guids of the records in a collection

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Eugen Podkorytov Profile Picture

Eugen Podkorytov 106

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 106 Most Valuable Professional

Overall leaderboard

Product updates

Dynamics 365 release plans