Showing posts with label Get & Set ActvityParty Entity & PartyList Fields in D365 C# Code. Show all posts
Showing posts with label Get & Set ActvityParty Entity & PartyList Fields in D365 C# Code. Show all posts

Friday, July 30, 2021

Get and Set activityparty entity and Partylist field using C# code in D365

This blog will give you overview on activity Party entity and partylist fields.

Within Dynamics 365 we have activity party entity as well as partylist fields, an activity party represents a person or group associated with an activity. An activity can have multiple activity parties. 
There are 11 activity party types in Dynamics 365 for Customer Engagement (Sender "From", ToRecipient, CCRecipient, BccRecipient, RequiredAttendee, OptionalAttendee, Organizer, Regarding, Owner, Resource, Customer).

CRM Activity PartyList field is a special type of lookup field which refers to multiple entities. To hold or store these multiple entities references, Microsoft has provided one special type of entity named ActivityParty.

ActivityParty, has one special field called PartyId. which has the capability to store multiple entities references like user, contact, account, lead, queue etc.

Hence, if you want to get/set value in PartyList Field, you need to interact with ActivityParty PartyId field.

Remember, while getting or setting value in party list fields (to, from cc, bcc, regarding etc). You should use ActivityParty Entity and PartyId field. 

1. To get the value from Party List TO and FROM fields:

Retrieve the value from PartyId field of ActivityParty entity, which holds the record’s Guids which was selected in Email’s FROM and TO fields.

2. To set the value in Party List TO and FROM fields:

First pass the value in PartyId field of ActivityParty entity and then set it’s EntityReference to partylist FROM and TO fields.

Note: Use the dynamics value in place of hardcoded values.

Get partyList fields value using C# code:

public static void GetPartyList(string emailRecordId)
        {
            emailRecordId = "C472398F-592F-E561-A939-000S4AF24369";
            // Retrieve email record
            Entity email = service.Retrieve("email", new Guid(emailRecordId), new ColumnSet("from", "to"));
 
            // get value from partylist - 'FROM' field
            EntityCollection from = email.GetAttributeValue<EntityCollection>("from");
            if (from != null && from.Entities.Count > 0)
            {
                foreach (var item in from.Entities)
                {
                    EntityReference partyId = item.GetAttributeValue<EntityReference>("partyid");
                    string addressUsed = item.GetAttributeValue<string>("addressused");
                }
            }
 
            // get value from partylist - 'TO' field
            EntityCollection to = email.GetAttributeValue<EntityCollection>("to");
            if (to != null && to.Entities.Count > 0)
            {
                foreach (var item in to.Entities)
                {
                    EntityReference partyId = item.GetAttributeValue<EntityReference>("partyid");
                    string addressUsed = item.GetAttributeValue<string>("addressused");
                }
            }

        }

//-----------------------------------------------------------------------------------------------

Set value in Party List Fields using C# (Single Recipient)

//Set partyList field when there is single recipient
        public static void SetPartyList()
        {
            // Declare party list entity to set value in FROM & TO field
            Entity from = new Entity("activityparty");
            Entity to = new Entity("activityparty");
 
            // Set value in FROM & TO field
            from["partyid"] = new EntityReference("systemuser", new Guid("D7241D9E-FCEC-4C6B-80D0-A2C5B1588D5"));
            to["partyid"] = new EntityReference("account", new Guid("475B158C-4C6C-E511-8C63-72D8D9E47BA8"));
 
            // Declare party list entity to set value in FROM field
            Entity email = new Entity("email");
 
            // Insert value in email FROM & TO field
            email["from"] = new Entity[] { from };
            email["to"] = new Entity[] { to };
 
            //Set regarding object property (i.e. The entity record, which u want this email associated with)
            EntityReference regardingObject = new EntityReference("contact", new Guid("6C3BE98B-88DF-E311-B8E5-6HGE55B8B676"));
 
            email.Attributes.Add("regardingobjectid", regardingObject);
 
            //Set subject & body properties
            email.Attributes.Add("subject", "Email subject created for Single recipients");
            email.Attributes.Add("description", "This is Email description for Single recipients");
 
            //Create email activity
            Guid emailID = service.Create(email);

        }

Set value in Party List Fields using C# (Multiple Recipient)

//Set partyList field when there is Multiple recipient

        public static void SetPartyList()
        {
            // Set value in FROM party list field
            Entity from1 = new Entity("activityparty");
 
            // two accounts inside the TO field
            Entity to1 = new Entity("activityparty");
            Entity to2 = new Entity("activityparty");
            // two contacts inside the TO field
            Entity to3 = new Entity("activityparty");
            Entity to4 = new Entity("activityparty");
 
            // set value in FROM field
            from1["partyid"] = new EntityReference("systemuser", new Guid("D72D8D9E-FCEC-4C6B-8340-A2CB9FAA88D5"));
 
            // set multiple values in TO field
            to1["partyid"] = new EntityReference("account", new Guid("475B158C-541C-E511-80D3-3863BB347BA8"));
            to2["partyid"] = new EntityReference("account", new Guid("A8A19CDD-88DF-E311-B8E5-6C3BE5A8B200"));
            to3["partyid"] = new EntityReference("contact", new Guid("25A17064-1AE7-E611-80F4-E0071B661F01"));
            to4["partyid"] = new EntityReference("contact", new Guid("48A0E5B9-88DF-E311-B8E5-6C3BE5A8B980"));
            Entity email = new Entity("email");
 
            //Set regarding object property (i.e. The entity record, which u want this email associated with)
            EntityReference regardingObject = new EntityReference("contact", new Guid("48A0E9B9-88DF-E311-B8E5-6C3BE5A8B980"));
 
            email.Attributes.Add("regardingobjectid", regardingObject);
 
            // Insert value in FROM & TO field
            email["from"] = new Entity[] { from1 };
            email["to"] = new Entity[] { to1, to2, to3, to4 };
 
            //Set subject & body properties
            email.Attributes.Add("subject", "Email created from Console for Multiple recipients");
            email.Attributes.Add("description", "Email created from Console or Multiple recipients");
 
            //Create email activity
            Guid emailID = service.Create(email);

        }


References: Click HereMS Docs ActivityParty

Thank you!!!

Keep CRMing... 

Happy Learning.....