0

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?

Jesper Hoff
  • 294
  • 4
  • 15

1 Answers1

1

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;
            }

        }
    }
}
Martin Davies
  • 4,436
  • 3
  • 19
  • 40
  • Thank you very much! That solved it for me. I fiddled a bit with the Sitecore.Analytics.Data.TrackingField class, to see if I could use existing code instead of hacking into the XML, but for some reason, its profiles are write protected so your way seems to be the one to go through :) – Jesper Hoff May 27 '14 at 07:07