Monday, May 21, 2012

MS CRM 2011: Import of marketing list members using standard import with small extensions

Unfortunately at the moment it is impossible to import Marketing Lists Members to CRM with out-of-box import. My friend, former MVP and employee of Microsoft Artem Grunin provided good approach how to eliminate this problem with small customizations and plugin.

Main idea is:

1. Create custom entity List Member.

2. Add relationships between List Member (Custom) and entities Marketing List, Lead, Account and Contact.

3. Develop plugin which will handle Create operation of List Member and Add members to correspond list.

Create solution:

Add Account, Contact, Lead and Marketing list entities to it:

Create custom entity List Member and add required relationships to Contact, Account, Lead and Marketing List:


After all relationships were added you should have something similar to following:

Customizations part is finished. Next part is plugin. Open Visual Studio and create new project based on template, create new Plugin to handle creation of List Member. Here is code for this plugin:


namespace XrmSolutions.MarketingListImport.Plugins
{
    using System;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Crm.Sdk.Messages;

    public class PreCreateListMember: Plugin
    {
        public PreCreateListMember()
            : base(typeof(PreCreateListMember))
        {
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>
        (10, "Create", "xrm_listmember", new Action<LocalPluginContext>(ExecutePreCreateListMember)));
        }

        protected void ExecutePreCreateListMember(LocalPluginContext localContext)
        {
            if (!localContext.PluginExecutionContext.MessageName.Equals("Create", StringComparison.InvariantCultureIgnoreCase))
                return;

            Entity importedRecord = localContext.PluginExecutionContext.InputParameters["Target"] as Entity;

            if (!importedRecord.LogicalName.Equals("xrm_listmember"))
                return;

            if (!importedRecord.Contains("xrm_listid"))
                throw new InvalidPluginExecutionException("List was not mapped!");

            EntityReference listitem = null;

            if (importedRecord.Contains("xrm_leadid"))
                listitem = importedRecord.GetAttributeValue<EntityReference>("xrm_leadid");
            else if (importedRecord.Contains("xrm_accountid"))
                listitem = importedRecord.GetAttributeValue<EntityReference>("xrm_accountid");
            else if (importedRecord.Contains("xrm_contactid"))
                listitem = importedRecord.GetAttributeValue<EntityReference>("xrm_contactid");
            else
                throw new InvalidPluginExecutionException("Contact, Account and Lead fields don't contain information!");

            AddMemberListRequest request = new AddMemberListRequest()
            {
                ListId = importedRecord.GetAttributeValue<EntityReference>("xrm_listid").Id,
                EntityId = listitem.Id
            };

            localContext.OrganizationService.Execute(request);
        }
    }
}


Demonstration – I have create 2 test accounts Import Test 1 and Import Test 2 and 2 lists – List 1 and List 2 as it shown and I have create import file which is provided table:


ListAccount
List 1Import Test 1
List 2Import Test 2

After you can import this file to CRM using standard CRM Import:









And result - form of Marketing List - List 1:


Here are source code and solution: