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