Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

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

  • Suggested answer
    Alagunellaikumar Profile Picture
    Alagunellaikumar 6,210 on at
    RE: How can i add More than one TO value in Email Entity?

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

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How can i add More than one TO value in Email Entity?

    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
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: How can i add More than one TO value in Email Entity?

    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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans