Power Community

Power Community

Add Members to static marketing list from excel file to Dynamics 365 using C#

https://1.gravatar.com/avatar/a6eb8b847654f0fe82747fd4b0c56be2?s=96&d=identicon&r=G

Hi,

Happy new year 2023….

To start with this scenario, this is one of the most commonly asked functionalities when we talk about of adding members to marketing list using an excel import.

In this scenario, we will make use of Connections entity and associate the marketing list to the contacts on creation of the connection…ok…let us give it a try….we will make use of plugin here….which runs on Create and on Post operation and calls the below method…

 public void MarketingListImport()
        {
            Entity connectionER = (Entity)executionContext.InputParameters["Target"];
            Entity marketingList = new Entity();
            if (connectionER.Contains("record2id"))
            {
                EntityReference connectTo = connectionER.GetAttributeValue("record2id");
                EntityReference connectFrom = connectionER.GetAttributeValue("record1id");
                if (connectTo != null)
                {
                    marketingList = organizationService.Retrieve(connectTo.LogicalName, connectTo.Id, new ColumnSet("type", "createdfromcode"));
                }
                if (connectTo.LogicalName == "list")//Marketing list 
                {
                    if (marketingList.Contains("type") && marketingList.GetAttributeValue("type") == false)
                    {
                        if (connectFrom.LogicalName == "contact" && marketingList.Contains("createdfromcode") && marketingList.GetAttributeValue("createdfromcode").Value == 2)//Resident
                        {
                            // Add a list of contacts to the marketing list.
                            var addMemberListReq = new AddListMembersListRequest
                            {
                                MemberIds = new[] { connectFrom.Id },
                                ListId = connectTo.Id
                            };
                            organizationService.Execute(addMemberListReq);
                            tracingService.Trace("Contacts with GUIDs rnt{{{0}}} is added to the list.",
                                connectFrom.Id);
                        }
                        else
                        {
                            throw new InvalidPluginExecutionException("Marketing list should be targeted at contact and contacts can be imported");
                        }
                    }
                    else
                    {
                        throw new InvalidPluginExecutionException("Members cannot be imported into a Dynamic Marketing list, please select a static marketing list and try again");
                    }
                }
            }
        }
    }

Once you were done writing the code, next step is to test the functionality…for this we will use the following template.

Import this template from the imports area under settings in your Dynamics 365…boom….the contacts in the template will be added to your Static Marketing List.

Cheers,

PMDY

Advertisement

Author: Pavan Mani Deep Y

Technology geek who loves sharing my leanings, quick tips and new features on Dynamics 365 & related tools, technologies. An Azure IOT Enthusiast….

This post was originally published on this site

- Advertisement -spot_img

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement - Advertisement

Latest News

On-premises data gateway September 2023 release

Welcome to September! Here is the September 2023 on-premises data gateway release (version 3000.190.17). This update brings the on-premises...

More Articles Like This

- Advertisement -spot_img