Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / ECELLORS CRM Blog / Add Members to static marke...

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

pavanmanideep Profile Picture pavanmanideep 1,331

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<EntityReference>("record2id");
                EntityReference connectFrom = connectionER.GetAttributeValue<EntityReference>("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<bool>("type") == false)
                    {

                        if (connectFrom.LogicalName == "contact" && marketingList.Contains("createdfromcode") && marketingList.GetAttributeValue<OptionSetValue>("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 \r\n\t{{{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


This was originally posted here.

Comments

*This post is locked for comments