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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How can i add More than one TO value in Email Entity?

(0) ShareShare
ReportReport
Posted on by 1,000

I use the following code for add the userid in Email Entity To's field.If I enter the single value (a single user) in to field the following code will working fine.

Now, I need to add more than one user id in TO field which means i need to send multiple emails at a time.How can I add multiple to fields in CRM email Entity.

Entity email = new Entity();
EntityReference to = new EntityReference("systemuser", new Guid("<Guid Here>"));

//Derive to party
Entity toParty = new Entity("activityparty");
toParty.Attributes.Add("partyid", to);

EntityCollection collToParty = new EntityCollection();
collToParty.EntityName = "systemuser";
collToParty.Entities.Add(toParty);

email.Attributes.Add("to", collToParty);

 

Thanks in advance.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Vijay,

    Please try the following code.

    // Create a new activity party linked to a system user
    Entity party1 = new Entity("activityparty");
    party1["addressused"] = "some@email.com";
    party1["partyid"] = new EntityReference("systemuser", systemUserId);
    
    // Create a new unresolved activity party
    Entity party2 = new Entity("activityparty");
    party2["addressused"] = "unresolved@email.com";
    
    // Create a new EntityCollection and add the 2 parties
    EntityCollection to = new EntityCollection();
    to.Entities.Add(party1);
    to.Entities.Add(party2);
    
    // Create an email with the EntityCollection
    Entity email = new Entity("email");
    email["subject"] = "Test Party Lists";
    email["to"] = to;
    
    Guid emailId = service.Create(email);

    For more details, refer magnetismsolutions.com/.../working-with-dynamics-crm-activity-party-lists-in-c--plugins

    Hope this helps you.

  • Suggested answer
    Community Member Profile Picture
    on at

    Refer below code:

     //get security role id from input
                    Guid secRoleId = SecurityRole.Get<EntityReference>(executionContext).Id;
    
                    //get users from the security role input
                    EntityCollection users = GetUsersFromSecurityRole(secRoleId, service);
    				
    				 if (users != null)
                        {   
                            List<Entity> ToList = new List<Entity>();
                            // General List of Users in Security Role
                            ToList = ProcessUsers(users, ToList);
    
                            string emailSubject = "URGENT ALARM. This scheduled medication is overdue by more than TWO HOURS";
    
                            string emailBody = "<p style=\"font-family:calibri; font-size:12px\">URGENT ALARM. This scheduled medication is overdue by more than TWO HOURS. Please consider creating a WHS Incident.</p><br>" +
                                                "<a style=\"font-family:calibri; font-size:12px\" href=" + activityOfDailyLivingURL + ">Hyperlink to Activity Of Daily Living Record</a>";
                            // Derive email sender
                            EntityReference emailSender = this.EmailSender.Get(executionContext);
                            
                            // Send Email
                            this.SendEmail(service, emailSubject, emailBody, emailSender, ToList, activityOfDailyLiving);
                        }
    					
    					private void SendEmail(IOrganizationService service, string emailSubject, string emailBody, EntityReference emailSender, List<Entity> ToList, Entity activityOfDailyLiving)
            {
                Entity email = new Entity();
                email.LogicalName = "email";
    
                EntityReference regardingObjectId = new EntityReference(activityOfDailyLiving.LogicalName, (Guid)activityOfDailyLiving.Id);
    
                EntityReference from = new EntityReference(emailSender.LogicalName, emailSender.Id);
    
                //Derive from party
                Entity fromParty = new Entity("activityparty");
                fromParty.Attributes.Add("partyid", from);
    
                EntityCollection collFromParty = new EntityCollection();
                collFromParty.EntityName = "systemuser";
                collFromParty.Entities.Add(fromParty);
    
                email.Attributes.Add("from", collFromParty);
                email.Attributes.Add("to", ToList.ToArray());
    
                //Set subject & body properties
                email.Attributes.Add("subject", emailSubject);
                email.Attributes.Add("description", emailBody);
                email.Attributes.Add("regardingobjectid", regardingObjectId);
    
                //Create email activity
                Guid emailID = service.Create(email);
    
                try
                {
                    //Sending email
                    SendEmailRequest reqSendEmail = new SendEmailRequest();
                    reqSendEmail.EmailId = emailID;//ID of created mail
                    reqSendEmail.TrackingToken = "";
                    reqSendEmail.IssueSend = true;
    
                    SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail);
                }
                catch
                { }
            }
    
            private List<Entity> ProcessUsers(EntityCollection users, List<Entity> ToList)
            {
                foreach (Entity e in users.Entities)
                {
                    Entity activityParty = new Entity("activityparty");
                    activityParty["partyid"] = new EntityReference("systemuser", e.Id);
    
                    if (ToList.Any(t => t.GetAttributeValue<EntityReference>("partyid").Id == e.Id)) continue;
    
                    ToList.Add(activityParty);
                }
    
                return ToList;
            }
    		
    		 private EntityCollection GetUsersFromSecurityRole(Guid secRoleId, IOrganizationService service)
            {
                //Query for the users with security role
                QueryExpression query = new QueryExpression
                {
                    EntityName = "systemuser",
                    ColumnSet = new ColumnSet("systemuserid"),
                    LinkEntities = 
                    {
                        new LinkEntity
                        {
                            LinkFromEntityName = "systemuser",
                            LinkFromAttributeName = "systemuserid",
                            LinkToEntityName = "systemuserroles",
                            LinkToAttributeName = "systemuserid",
                            LinkCriteria = new FilterExpression
                            {
                                FilterOperator = LogicalOperator.And,
                                Conditions = 
                                {
                                    new ConditionExpression
                                    {
                                        AttributeName = "roleid",
                                        Operator = ConditionOperator.Equal,
                                        Values = { secRoleId }
                                    }
                                }
                            }
                        }
                    },
                    Criteria = new FilterExpression
                    {
                        Conditions =
                        {
                            new ConditionExpression
                            {
                                AttributeName = "internalemailaddress",
                                Operator = ConditionOperator.NotNull
                            }
                        }
                    }
                };
    
                return service.RetrieveMultiple(query);
            }

  • Suggested answer
    Alagunellaikumar Profile Picture
    6,212 on at

    Hi

    Create a another activity party and add the object to the collToParty  collection, please refer below code

    //Derive to party(1st Party)

    Entity toParty = new Entity("activityparty");

    toParty.Attributes.Add("partyid", to);

    EntityCollection collToParty = new EntityCollection();

    collToParty.EntityName = "systemuser";

    collToParty.Entities.Add(toParty);

    //2nd Party

    Entity 2ndtoParty = new Entity("activityparty");

    2ndtoParty .Attributes.Add("partyid", to);

    collToParty.Entities.Add(2ndtoParty );

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans