web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
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

I have the same question (0)
  • Verified answer
    David Jennaway Profile Picture
    14,065 on at

    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
  • Anthony Dong Profile Picture
    Microsoft Employee on at

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

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    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);
                }

  • FikriHailal Profile Picture
    121 on at

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

  • gdas Profile Picture
    50,091 Moderator on at

    Its seems  dtableUserQuality. table has duplicate entry.

  • FikriHailal Profile Picture
    121 on at

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

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

#2
ManoVerse Profile Picture

ManoVerse 55 Super User 2026 Season 1

#3
11manish Profile Picture

11manish 37

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans