1

The client has a requirement where they want to automate content profiling based on the meta keywords. For example, if the user creates a content item and add meta keyword as "Cycle" then while saving an item that Item should automatically associate with corresponding Profile like "Cyclist". The author is not required to associate profile manually in the Experience editor.

I have tried to update Profile in the tracking field on an item but it seems to be read-only.

Can anyone help me?

4
  • 2
    Did you try this? hishaamn.wordpress.com/2017/05/28/…
    – x3mxray
    Commented Jul 8, 2019 at 10:27
  • Perfect. It worked. Commented Jul 8, 2019 at 12:03
  • @x3mxray Can you take the notes from Hishaamn's post and make it an answer. So we don't lose it if his blog goes away
    – Chris Auer
    Commented Jul 8, 2019 at 22:49
  • 1
    @ChrisAuer Done.
    – x3mxray
    Commented Jul 9, 2019 at 14:13

2 Answers 2

3

Notes with code from Hishaamn Namooya blog post:

The TagProfile method retrieves the different profile presets (Profile Cards) and then update the tracking field of content item accordingly with the Profile Keys:

public void TagProfile(Item contentItem, Guid profileId)
{
    // retrieve tracking information 
    ProfileUtil.GetProfiles(contentItem, out TrackingField trackingField);
    var trackingValue = contentItem.Fields["__Tracking"].Value;

    // get needed profile
    var profile = trackingField.Profiles.FirstOrDefault(p => p.ProfileID.Guid.Equals(profileId));
    if (profile != null)
    {
        var profileItem = new ProfileItem(profile.GetProfileItem());

        var dictionary = new Dictionary<string, double>();
        // TODO: implement logic that you need here
        // iterate through profile cards and add NEEDED key/values to presets dictionary
        foreach (var card in profileItem.ProfileCards)
        {
            dictionary.Add(card.InnerItem.Key, 10);
        }

        profile.SaveToField = true;
        profile.Presets = dictionary;

        SetProfileKeys(profile, profileItem);

        TrackingField.UpdateKeyValues(profile);
    }

    // update "Tracking" field raw-value of contentItem

    XDocument document = string.IsNullOrEmpty(trackingValue) ? new XDocument(new XElement("tracking")) : XDocument.Parse(trackingValue);
    XElement xelement1 = document.Element("tracking");
    if (xelement1 == null)
    {
        xelement1 = new XElement("tracking");
        document.Add(xelement1);
    }
    List<XElement> list = xelement1.Elements("profile").ToList();

    foreach (XElement t in list)
    {
        t.Remove();
    }

    foreach (XElement xelement2 in trackingField.Profiles.Where(x => x.SaveToField).Select(x => x.ToXElement()))
        xelement1.Add(xelement2);

    // save
    using (new SecurityDisabler())
    {
        contentItem.Editing.BeginEdit();
        contentItem.Fields["__Tracking"].Value = document.ToString();
        contentItem.Editing.EndEdit();
    }
}

The SetProfileKeys method is used to set the different profile keys:

public void SetProfileKeys(ContentProfile profile, ProfileItem profileItem) 
{ 
   foreach (ProfileKeyItem profileKeyItem in profileItem.Keys.ToList()) 
   { 
       ProfileKeyItem keyItem = profileKeyItem; 
       if (profile.Keys.FirstOrDefault(k => string.Compare(k.Key, keyItem.Name, StringComparison.InvariantCultureIgnoreCase) == 0) == null) 
       { 
           ContentProfileKeyData key = new ContentProfileKeyData(keyItem) 
           { 
                Value = keyItem.GetDefaultValue() 
            }; 
            profile.AddKey(key); 
        } 
    } 


    foreach (ContentProfileKeyData key in profile.Keys.Where(key => key.ProfileDefinitionId == Guid.Empty)) 
    { 
        profile.RemoveKey(key); 
    } 

    TrackingField.UpdateKeyValues(profile); 
} 
0

Below blog explains exacts steps to achieve this functionality.

https://hishaamn.wordpress.com/2017/05/28/tag-sitecore-content-with-profile-card-programmatically/

I have created Persona Tags and modified Persona Template to refer it as multi-list. I have customized OnItemsave Pipeline to update Profile in content Item depending on Meta keywords(Which is mapped to Persona Tags) used.

Thanks to Hishaamn Namooya.

1
  • While I appreciate the answer, we try and avoid link only answers. If that link goes away, the answer is invalid. This is why I requested x3mxray create the blog in an answer.
    – Chris Auer
    Commented Jul 9, 2019 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.