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:

2 comments:

  1. Interesting post. I implemented it in a different way. The company for which I built the solution usually imports event visitors out of Excel file and then wants to generate email blast with "Thank you" message. So, the approach was the following: first import visitors as Campaign Responses, next using written WebApp we process these Campaign Responses and check by email if such a person exists in the system if found it is added to the created MarketingList if not then is is first created and only then added.

    ReplyDelete
  2. I agree, this is an interesting solution to this problem. I came up with a slightly different way yet again to implement this by using a combination of a Custom entity, a dynamic Marketing list and a conversion to a Static Marketing list. I wrote about it here if it's of interest to you: Importing Marketing List Members

    ReplyDelete