Archive for August, 2014

Programmatically access Sitecore item presentation

During regular use, we usually access the presentation of an item through sitecore, but at times, you might need to be able to do this through your back end code.

In our scenario, we had various frame sublayouts which we used throughout the site to maintain page structure.
The front end required a certain class to be added to the top level body tag which resided in the layout file, only when a certain frame sublayout was in use.

Not wanting to achieve this through javascript since this was a fairly large dependency – front end wise, we decided to examine the presentation of the context item programmatically to determine if the given frame sublayout was a part of it. (Using the __Renderings field we could access the presentation of a given item) This code was executed in the layout file.
Mind you, this code is only meant for the default first device in presentation.

The following method can be used to determine if a given sublayout is a part of the presentation of the ‘currentItem‘ passed into the method.

        public class LayoutHelper
            /// <summary>
            /// Checks if any of the sublayouts specified in 'sublayoutGuids' is present in the presentation of the 'currentItem'
            /// </summary>
            /// <param name="currentItem">The current item.</param>
            /// <param name="sublayoutGuids">The sublayout guids.</param>
            /// <returns>true / false</returns>
            public static bool AnySublayoutInPresentation(Item currentItem, List<string> sublayoutGuids)
                var layout = LayoutDefinition.Parse(LayoutField.GetFieldValue(currentItem.Fields["__Renderings"]));

                if (layout.Devices != null && layout.Devices.Count > 0)
                    var dev = (DeviceDefinition)layout.Devices[0];
                    if (dev != null)
                        var renderings = dev.Renderings;
                        if (renderings != null)
                            if (renderings.Cast<RenderingDefinition>().Any(rend => sublayoutGuids.Contains(rend.ItemID)))
                                return true;
                return false;

, , ,

Leave a comment

Programmatically trigger Sitecore DMS goals

Sitecore provides you with a robust way of configuring when a DMS goal is triggered using the rules editor.
Its usage is quite intuitive and there are a vast number of options available for the configuration.

But at times depending on the site specific custom business logic we have in place, we might want to be able to trigger a goal from our code.

Here is how we would do that.
Following is the Demo Goal I have created and am going to work with:

To trigger the goal from code:

            if (!Sitecore.Analytics.Tracker.IsActive)

            if (!Sitecore.Analytics.Tracker.IsActive 
                || Sitecore.Analytics.Tracker.CurrentPage == null) 

            var goalItem = Sitecore.Context.Database.GetItem("{710AC40A-EFC3-43AE-B7F8-225716D73EC9}");
            var goal = new PageEventItem(goalItem);
            var pageEventsRow = Sitecore.Analytics.Tracker.CurrentPage.Register(goal);

            pageEventsRow.Data = "Demo goal test information : " 
                + DateTime.Now.ToString("F");
            pageEventsRow.ItemId = Sitecore.Context.Item.ID.Guid;
            pageEventsRow.DataKey = Sitecore.Context.Item.Paths.Path;



using Sitecore.Analytics.Data.Items;

Getting a report of the goals triggered is not really straightforward when you use the Sitecore Engagement Analytics option.
You could instead directly fire a query on the analytics database to see immediate results with ease.

SELECT Name, DataKey, ItemId, Data, 
pe.PageEventDefinitionId AS GoalItemId, Value AS GoalValue, 
VisitorId, VisitId, [DateTime] 
FROM dbo.PageEvents pe, dbo.PageEventDefinitions ped
WHERE pe.PageEventDefinitionId = ped.PageEventDefinitionId
AND ped.IsGoal = 1
AND ped.Value!=0


, , , ,

1 Comment