Closing this message and/or accessing our website tells us you are happy to receive all cookies on the ClearPeople website.
However, if you would like to, you can change your cookies settings at any time.
Starting from Sitecore 9, Dynamics connector for Data Exchange Framework does not synchronize Dynamics CRM Marketing Lists any more, nor provides a segmentation condition to choose the marketing list to apply the segment to. This is how we solved it.

This article is part of a series about Dynamics CRM synchronisation in Sitecore 9:

If you haven't read the first article in the series, we recommend you to do so before going through this one, as all the settings related with the CRM connector are defined there.

 

The problem

As we detailed in the previous post, Dynamics CRM Marketing Lists synchronisation is no longer part of the Dynamics CRM connector, starting from Sitecore 9, so we cannot create a segmented list with all the contacts included in a specific CRM Marketing List.

 

The solution

Continuing with the solution introduced in the previous post, we left it in a point where we managed to get the Dynamics lists into Sitecore items, so we can now create a segmentation rule to filter contacts using his information.

 

To deal with all the code and related items, we use the Helix principles, and added a new Feature project for all the CRM related personalisation.

 

1. Read the Dynamics Membership facet

Firstly, we need to get the information from the custom facet that the Dynamics connector added during the synchronisation process. We can easily create an extension for Contacts with this code:


using Sitecore.DataExchange.Tools.DynamicsConnect.Facets;
using Sitecore.XConnect;

namespace CP.Feature.CRM.Extensions
{
    public static class ContactExtensions
    {
        public static DynamicsMembership DynamicsMembership(this Contact c)
        {
            return c.GetFacet<DynamicsMembership>(Sitecore.DataExchange.Tools.DynamicsConnect.Facets.DynamicsMembership.DefaultFacetKey);
        }
    }
}

2. Create the segmented list condition

Now, let's create the code that will evaluate the contact in the segmented list:


using CP.Feature.CRM.Extensions;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Framework.Rules;
using Sitecore.XConnect;
using Sitecore.XConnect.Segmentation.Predicates;
using System;
using System.Linq;
using System.Linq.Expressions;
using Sitecore.Diagnostics;

namespace CP.Feature.CRM.Conditions
{
    public class MarketingListConditionForSegmentation : ICondition, IMappableRuleEntity, IContactSearchQueryFactory
    {
        public Guid ListId { get; set; }

        private string _listId { get; set; }

        private String Id
        {
            get
            {
                if (!string.IsNullOrEmpty(_listId))
                    return _listId;

                var value = string.Empty;
                try
                {
                    Database database = Sitecore.Data.Database.GetDatabase("master");
                    Item myItem = database.GetItem(ListId.ToString());
                    if (myItem != null && myItem.Fields[Templates.External_Marketing_List.Fields.MarketingListId] != null)
                        value = myItem.Fields[Templates.External_Marketing_List.Fields.MarketingListId].Value.ToLowerInvariant();

                    _listId = value;

                    return value;
                }
                catch (Exception ex)
                {
                    Log.Info("[MarketingListConditionForSegmentation] ERROR: " + ex.Message, this);
                    return value;
                }
            }
        }

        public MarketingListConditionForSegmentation()
        {
        }

        public Expression<Func<Contact, bool>> CreateContactSearchQuery(IContactSearchQueryContext context)
        {
            return (Contact contact) => contact.DynamicsMembership().MarketingListIds.Any<string>(s => s == Id);
        }

        public bool Evaluate(IRuleExecutionContext context)
        {
            var listSubscription = context.Fact<Contact>(null).DynamicsMembership();
            if (listSubscription != null && listSubscription.MarketingListIds != null && listSubscription.MarketingListIds.Any())
            {
                return false;
            }
            return listSubscription.MarketingListIds.Any<string>(s => s == Id);
        }
    }
}

 

You may need to install some packages from the Sitecore Nuget feed.

 

3. Create the condition in Sitecore

Once the code has been created and deployed to Sitecore, we can define our custom rule to use it, below /sitecore/system/Settings/Rules/Definitions/Elements/

 

We have created a separate folder for "Dynamics", but you can add it to any existing folder.

Tags folder

 

Create a new rule "Contact is member of CRM List", with the following fields values:

Field

Value

Text

where the contact is a member of CRM Marketing List [ListId,Tree,root={2E84250C-DB4D-4871-9778-ACA70716D724}, list name]

Type

CP.Feature.CRM.Conditions.MarketingListConditionForSegmentation,CP.Feature.CRM

Where the id "{2E84250C-DB4D-4871-9778-ACA70716D724}" is the id of the root folder where your marketing lists items are stored in Sitecore (defined on step 5 in the previous article), and the type will be your assembly name and class.

 

Finally, in the case you create a new rules folder, make sure the Default item in Tags has the following tags selected:

  • XConnect - Condition
  • XConnect - Search Query

4. Use the condition in a segmented list

Now that our new condition is created, we can make use of it. Once we have run the "Dynamics Contacts to xConnect Sync" and the "Dynamics Marketing Lists to xConnect Sync", including our custom pipeline from previous post, we should have all our CRM contacts and lists synchronised into Sitecore.

 

From the List Manager or EXM Dashboard, let's create a new Segmented List from all contacts (or a Segment, to use later on another segmented list):

Segmented List from all contacts 

After giving the list a proper name, we can add a new segment to it:

Create new segment 

Give the segment a name, add a new segmentation rule and edit it:

Edit Rule 

From the available conditions, choose our custom defined condition for the Marketing Lists:

Condition selected 

Click on the "list name" option to choose from the list of Marketing Lists available:

Marketing List selection 

Now, after saving the Contact List created, you should see the included contacts already listed:

Contacts included 

Who should be the same contacts that have been included in the Dynamics list:

Contacts in Dynamics list  

 

Conclusion

So, finally, we managed to replicate the original functionality from Sitecore 8, and now our marketers can handle the marketing lists in Dynamics CRM and send the emails to those using Sitecore EXM.

Author bio

Sergi Gisbert
Sergio Gisbert
Technical Architect
I'm primarily focused on the delivery of successful projects with Sitecore CMS. When I’m not at work, I’m running a side project started back in 2009 called “Fatherhood," which takes up most of my non-working hours. I'm always keen to escape to snowy mountains and ski down them!

Comments


comments powered by Disqus

Related Articles

Sign up to our Newsletter

Every now and then, we'd like to send you information that delivers, develops and promotes our products and services that are relevant to you. Submitting your details tells us that you're OK with this and you also agree to our Privacy & Cookies policy. You can, of course, opt out of these communications at any time.

X
Closing this message and/or accessing our website tells us you are happy to receive all cookies on the ClearPeople website.
However, if you would like to, you can change your cookies settings at any time.
Read our policy