August 2015 » My Kentico EMS API Deep Dive Presentation
Aug 22

My Kentico EMS API Deep Dive Presentation

Introduction

A few people asked me to provide my presentation from the Kentico User Group webinar that I presented at this past week. I have attached the presentation to this blog post.  I think it went really well considering the amount of people that attended and the feedback that I received from it.

Remember there is a great tip in there for how to use the new Kentico 8.2 MacroRuleInstanceTranslator class to optimize your contact group rebuild time and lead scoring calculation time for when you have a large volume of contacts in your Kentico instance.

The deck is embedded and can be downloaded from this post after the jump. 

 

 

Kentico 8.2 EMS API Deep Dive 

 

MacroRuleInstanceTranslator Source Code

 

using System;
using CMS.DataEngine;
using CMS.Membership;
using CMS.OnlineMarketing;

internal class ContactIsDetroitLionsFanInstanceTranslator : IMacroRuleInstanceTranslator
{
    /// <summary>
    /// Translates ContactIsDetroitsLionFanMacro rule.
    /// </summary>
    public ObjectQuery<ContactInfo> Translate(MacroRuleInstance macroRuleInstance)
    {
        if (macroRuleInstance == null)
        {
            throw new ArgumentNullException("macroRuleInstance");
        }
        if (macroRuleInstance.MacroRuleName != "ContactIsDetroitLionsFan")
        {
            throw new ArgumentException("[ContactIsDetroitLionsFanTranslator.Translate]: Only macro rule instances of type ContactIsDetroitLionsFan can be translated");
        }

        var ruleParameters = macroRuleInstance.Parameters;
        // ruleParameters contains the data user enters in macro fields
        string paramIs = ruleParameters["_is"].Value;
        QueryOperator queryOperator = paramIs == "!" ? QueryOperator.NotEquals : QueryOperator.Equals;

        return ContactInfoProvider.GetContacts().Where("ContactIsDetroitLionsFan", queryOperator, true);
    }
}

 

MacroRuleMetadataLoader Source Code

 

using System;
using System.Collections.Generic;
using CMS.Base;
using CMS.DocumentEngine;
using CMS.OnlineMarketing;

[CustomMacroRuleMetadataLoader]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Attribute class that ensures the loading of custom macro rule translator.
    /// </summary>
    private class CustomMacroRuleMetadataLoaderAttribute : CMSLoaderAttribute
    {
        /// <summary>
        /// The system executes the Init method of the CMSModuleLoader attributes when the application starts.
        /// </summary>
        public override void Init()
        {
            MacroRuleMetadata metadata = new MacroRuleMetadata(
                "ContactIsDetroitLionsFan",
                new ContactIsDetroitLionsFanInstanceTranslator(),
                affectingActivities: new List<string>(0),
                affectingAttributes: new List<string>
                        {
                            // Recalculate when specific attributes change
                            "contactisdetroitlionsfan",
                            "contactisgreenbaypackersfan"
                        }); 
            MacroRuleMetadataContainer.RegisterMetadata(metadata);
        }

    }
}

 

You might also be interested in

[X]

Be the first to know about my latest Kentico Tips & Tricks!

Do not show again