9 December 2014

How to access profiles, profile keys through code

Last month I went to the Sitecore DMS fundamentals training as I wanted to have a taste of what and how marketing team will use all the tools available. Some really interesting materials and tools that we are coding for but unfortunately don't really use too often...

During the training session, we had a chat about Profile card, Profile keys and how we are assigning those to a content item. The example given there was for a Cycling Shop. We could create some profile keys for "Beginner", "Amateur" and "Professional". Obviously, you will have different product that will be for Professional: Helmets, Bikes and even GoPro (why not...). Well that gave me an idea for this blog posts and the following on:
  1. How to access those keys and Profile card when we access the item.
  2. How to retrieve Related items based on profile keys - we will continue with that on a later post...
Before starting, I just wanted to point you to a few blogs and posts which helped me a lot:


So first let see how the data are stored in the sitecore item:

When you assign the Profile card and keys in Sitecore you will  click on the "Edit Profile Card associated with the item":

  

Next you will certainly edit the profile you want to assign to the content item and edit the values


Once you have done that, you will notice that those data are stored on the Content Item. The field you are looking for is (enable the standard fields): Tracking ( - or __tracking)

 If you view it with the raw values, you will see that all the data are stored in XML format:

I have pasted the formated XML here for readability:


 < profile id="{1E05D43D-BF07-403D-893D-552FCFBF2BD0}" name="MyTest" presets="general|100||">
  < key name="Digital" value="2" />
  < key name="Traditional" value="2" />
 < /profile>

Since those data are in a field, that means we can access them throuh code... Here are a few methods to show you how you can retrieve the ContentProfiles or just the ContentProfiles name or if you prefer to get the profile Keys... The important thing here is to get the Field "__tracking" and cast it to a Sitecore.Analytics.Data.TrackingField. This field will give you all the required information you need for Campaigns, events, and Profiles... From this field, you should now be able to extract the different profiles associated with the item and then from each profile you can access every keys...

        /// 
        /// Return the list of all profiles for the content Item
        /// 
        /// 
        /// 
        public static IEnumerable GetProfiles(Item item)
        {
            if (!AnalyticsSettings.Enabled)
            {
                return new List();
            }

            Field field = item.Fields["__tracking"];
            if (field == null)
                return new List();

            ContentProfile[] profiles = (new TrackingField(field)).Profiles;
            if (profiles == null)
                return new List();

            return profiles.ToList();
        }

        /// 
        /// Return the list of all profile names associated with the content Item
        /// Where Keys are defined. This is to make sure we are not returning the profiles that are not assigned
        /// 
        /// 
        /// 
        public static IEnumerable GetProfileNames(Item item)
        {
            if (!AnalyticsSettings.Enabled)
            {
                return new List();
            }

            Field field = item.Fields["__tracking"];
            if (field == null)
                return new List();

            ContentProfile[] profiles = (new TrackingField(field)).Profiles;
            if (profiles == null)
                return new List();

            return profiles.Where(p => GetProfileKeys(p,1).Count()>0).Select(i => i.Name).ToList();
        }


        /// 
        /// Get the keys contains in the Content Profile with a minimum score
        /// 
        /// 
        /// 
        /// 
        public static IEnumerable GetProfileKeys( ContentProfile profile, int minimumScore)
        {
            var matchedKeys = new List();

            foreach (ContentProfileKeyData profileKey in profile.Keys)
            {
                if (profileKey.Value >= minimumScore)
                {
                    matchedKeys.Add(profileKey);
                }
            }
            return matchedKeys;
        }

        /// 
        /// Get the key names contains in the Content Profile with a minimum score
        /// 
        /// 
        /// 
        /// 
        public static IEnumerable GetProfileKeyNames(ContentProfile profile, int minimumScore)
        {
            var matchedKeys = new List();

            foreach (ContentProfileKeyData profileKey in profile.Keys)
            {
                if (profileKey.Value >= minimumScore)
                {
                    matchedKeys.Add(profileKey.Name);
                }
            }
            return matchedKeys;
        }

        /// 
        /// Get the Key Names that are define in the Item profiles with a minimum score
        /// 
        /// 
        /// 
        /// 
        public static IEnumerable GetProfileKeyNames(Item item, int minimumScore)
        {
            List keyNames = new List();

            IEnumerable profiles = GetProfiles(item);
            foreach (var profile in profiles)
            {
                keyNames.AddRange(GetProfileKeyNames(profile, minimumScore));
            }
            return keyNames.Distinct();
 
        }

Now on the next post, we will see how we could use that to get the related content items based on Profile Keys...

1 comment:

  1. wonderful information, I had come to know about your blog from my friend nandu , hyderabad,i have read atleast 7 posts of yours by now, and let me tell you, your website gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanks a ton once again,
    Regards, sitecore training in hyderabad

    ReplyDelete