Add and Remove Member from Marketing List Using Plugin in Dynamics 365

 Introduction

Managing marketing list memberships efficiently is crucial for CRM systems like Microsoft Dynamics 365, where marketing lists play a key role in organizing campaigns and communications. This article demonstrates how to use custom plugins to add and remove contacts from marketing lists, leveraging Dynamics 365's SDK messages for optimized performance and robust error handling.

Reference: 1 

Setup and Requirements

Before implementing the custom plugins, ensure you have access to the Microsoft Dynamics 365 environment with appropriate privileges to register plugins and modify marketing lists. You will need the Microsoft Dynamics CRM SDK and access to the IOrganizationService.

Implementing the Custom Plugin Functions

The plugin consists of two main functions:

  1. Adding Contacts to a Marketing List
  2. Removing Contacts from a Marketing List

Both functions use specific SDK messages designed for handling list memberships, which are more efficient and reliable than manual entity manipulation.

1. Adding Contacts to a Marketing List

This function uses the AddListMembersListRequest class to add one or more contacts to a specified marketing list. It handles exceptions to provide detailed error information.

private void AddContactToMarketingList(IOrganizationService service, ITracingService tracer, Guid contactId, Guid marketingListId)
{
    tracer.Trace("Adding contact to marketing list with ID: {0}", marketingListId);

    var addMemberRequest = new AddListMembersListRequest
    {
        ListId = marketingListId,
        MemberIds = new Guid[] { contactId }
    };

    try
    {
        service.Execute(addMemberRequest);
        tracer.Trace("Contact with ID {0} successfully added to marketing list ID {1}.", contactId, marketingListId);
    }
    catch (Exception ex)
    {
        tracer.Trace("Failed to add contact with ID {0} to marketing list ID {1}. Exception: {2}", contactId, marketingListId, ex.Message);
        throw new InvalidPluginExecutionException($"Error adding contact with ID {contactId} to marketing list ID {marketingListId}: {ex.Message}", ex);
    }
}

2. Removing Contacts from a Marketing List

This function uses the RemoveMemberListRequest class to remove a single contact from a marketing list, ensuring all operations are logged and exceptions are caught and detailed.

private void RemoveContactFromMarketing List(IOrganizationService service, ITracingService tracer, Guid contactId, Guid marketingListId)
{
    tracer.Trace("Attempting to remove contact with ID: {0} from marketing list with ID: {1}", contactId, marketingListId);

    var removeMemberRequest = new RemoveMemberListRequest
    {
        ListId = marketingListId,
        EntityId = contactId
    };

    try
    {
        service.Execute(removeMemberRequest);
        tracer.Trace("Contact with ID {0} successfully removed from marketing list ID {1}.", contactId, marketingListId);
    }
    catch (Exception ex)
    {
        tracer.Trace("Failed to remove contact with ID {0} from marketing list ID {1}. Exception: {2}", contactId, marketingListId, ex.Message);
        throw new InvalidPluginExecutionException($"Error removing contact with ID {contactId} from marketing list ID {marketingListId}: {ex.Message}", ex);
    }
}


Conclusion

Using these custom plugin functions to manage marketing list memberships not only simplifies the process but also enhances its reliability and traceability. This approach is particularly useful for Dynamics 365 environments where marketing activities are frequent and list management is critical to the success of marketing operations.

Deployment

After thorough testing, deploy these plugins into your Dynamics 365 environment through the Plugin Registration Tool or your preferred CI/CD pipeline, ensuring all dependencies and permissions are correctly configured.


No comments:

Post a Comment