To remove a contact from a marketing list in Dynamics 365 using a plugin

Step 1. Create a marketing list entity

Marketing list

Step 2. Create a contact record

Allam purushotham

Step 3. Add contact to the marketing list

Marketing list related

Marketing list contacts

Step 4. Plugin registration for removal

Register a plugin with Message Remove Member

Register new step

Step 5. Contact removal process

Select Contact and Click on Remove Associated Contact from the Marketing List

Marketing list remove

Step 6. Post removal actions

After a Contact is removed Associated Contact from the Marketing List Plugin will trigger

Contact associated view

Step 7. Plugin logic

The Record got updated the with Boolean field as True

Allam purushotham bool

Plugin code

using Microsoft.Xrm.Sdk;
using System;
namespace Plugins
{
    public class ManageContact : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(null);
            if (context.MessageName == "RemoveMember")
            {
                if (context.PrimaryEntityName == "list")
                {
                    var contactId = (Guid)context.InputParameters["EntityId"];
                    var marketingListId = (Guid)context.InputParameters["ListId"];
                    Entity record = new Entity();
                    var contact = new Contact(record, service);
                    contact.UpdateBoolFields(contactId);
                }
            }
        }
        private void UpdateBoolFields(Guid contactId)
        {
            var updateContact = new Entity("contact");
            updateContact.Id = contactId;
            updateContact.Attributes.Add("apy_bool", true);
            Service.Update(updateContact);
        }
    }
}

Unit test code

[TestMethod]
public void ContactRemovedFromMarketingList_DiassocateOfMarketingFormList()
{
    Entity contact = new Entity("contact", Guid.NewGuid());
    Entity diqSubscriptionList = new Entity("list", Guid.NewGuid());
    Entity reminderSubscriptionList = new Entity("list", Guid.NewGuid());
    Entity SubscriptionList_Path = new Entity("list", Guid.NewGuid());
    SubscriptionList_Path.Attributes.Add("listname", "SubscriptionList_Path");   
    Entity listMember = new Entity("listmember", Guid.NewGuid());
    listMember.Attributes.Add("listid", SubscriptionList_Path.ToEntityReference());
    listMember.Attributes.Add("entityid", contact.ToEntityReference());
    List<Entity> data = new List<Entity> { contact, diqSubscriptionList, SubscriptionList_Path, listMember };    
    XrmFakedContext context = new XrmFakedContext();
    context.Initialize(data);    
    XrmFakedPluginExecutionContext ctx = context.GetDefaultPluginContext();
    ctx.MessageName = "RemoveMember";
    ctx.PrimaryEntityName = "list";
    ctx.InputParameters.Add("EntityId", contact.Id);
    ctx.InputParameters.Add("ListId", diqSubscriptionList.Id);   
    context.ExecutePluginWith<Plugins.ManageContact>(ctx);    
    Entity actualContact = context.CreateQuery("contact").Where(x => x.Id == contact.Id).First();   
    Assert.AreEqual(actualContact.GetAttributeValue<bool>("apy_bool"), true);
}