Search code examples
sitecoredms

Set DMS Profile Card programatically during publish in Sitecore


I need to attach a DMS Profile card to a sitecore item during publish, based on a set of criteria.

How do I do that?


Solution

  • I deleted my original answer and have posted this instead.

    This is a rough and ready, completely untested class, which is a processor to be added to the publishItem pipeline. (See Intercept Item Publishing with the Sitecore ASP.NET CMS)

    It's based on code found in the Sitecore Powershell project that achieves a similar result: Console / PowerShellIntegrations / Commandlets / Analytics / SetAnalyticsProfileCardCommand.cs

    Notice that you'll have to add the logic that selects which profile card to use.

    using Sitecore.Data;
    using Sitecore.Data.Items;
    using Sitecore.Publishing.Pipelines.PublishItem;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    
    namespace SC7Test.Business.Processors
    {
        class SetProfileCard : PublishItemProcessor
        {
            public override void Process(PublishItemContext context)
            {
                var db = Sitecore.Configuration.Factory.GetDatabase("master");
                var publishItem = db.GetItem(context.ItemId);
    
                // Add your logic here.
                var profileCardItem = db.GetItem("One of several profile card item ID's based on your selection logic");
                var profileCardValue = profileCardItem["Profile Card Value"];
    
                var doc = new XmlDocument();
                doc.LoadXml(profileCardValue);
    
                if (doc.DocumentElement != null && doc.GetElementsByTagName("profile").Count > 0 &&
                    doc.GetElementsByTagName("profile")[0] != null)
                {
    
                    XmlNode xmlNode = doc.GetElementsByTagName("profile")[0];
    
                    if (xmlNode.Attributes != null)
                    {
                        XmlAttribute presetAttribute = xmlNode.Attributes["presets"] ?? doc.CreateAttribute("presets");
    
                        presetAttribute.Value = profileCardItem.Name.ToLower() + "|100||";
                        xmlNode.Attributes.Append(presetAttribute);
                    }         
                }
    
                using (new EditContext(publishItem, false, false))
                {
                    publishItem["__Tracking"] = doc.InnerXml;
                }
    
            }
        }
    }