Experiences with Sitecore Federated Experience Manager

Over the past couple of days I’ve been a little fun with Federated Experience Manager and I’m really liking it 🙂 There’s a great deal of functionality and fun (yes I’m like that!) to be had. Especially with the concept of “hooking into” a completely independent site and tracking what pages are visit or even the concept of pushing content to other sites (we’ll explore that concept in a future blog post)

In this blog post I’ll quickly demonstrate how to quickly set up Federated Experience Manager and get started with the basics like tracking analytics.

How do I install Federated Experience Manager?

For my “independent” site I simply created a new Web Application (that contains no functionality – it’s not needed for this purpose) in Visual Studio. For this example I used ASP.NET MVC Application. What’s important to remember is that as it’s independent site to Sitecore it’s actually technology agnostic – you could have set up a demonstration in PHP or even straight HTML :).

So, once my independent site is set up it looks something like this:


As you can see, I’ve set this up to respond under http://testsite (IIS/hosts files have been configured etc.)

Now we can do the fun but and add a reference to this site in Fereration Experience Manager by adding an “External Site”:


There’s not a great deal of information to fill in here – what is very important is the “Tracking Script” (or Tracking Beacon). This is a piece of Javascript that you need to include in your independent site where you want the Federated Experience Manager to hook into. Without this script, there will be no interaction between Sitecore and your external site.

As my site is as out-of-the-box ASP.NET MVC site, I included this tracking script in the <head> element of _SiteLayout.cshtml:

<meta charset="utf-8" />
<title>@Page.Title – My ASP.NET Web Page</title>
<link href="~/Content/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />
<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery-ui-1.10.3.js"></script>
<script src="~/Scripts/modernizr-2.6.2.js"></script>
<!– Tracking Beacon inserted from Sitecore Federation Experience Manager –>
<script src="//sitecore8-150223/bundle/beacon"></script>
<!– End of Tracking Beacon script –>
<meta name="viewport" content="width=device-width" />

view raw
Tacking Beacon
hosted with ❤ by GitHub

Now that we have the Tracking Script configured on our site, we should be in a position to go to the Federated Experience Manager, select our Test Site and click the “Open in Experience Editor” button. You should see the following appear:


That made me smile 🙂 My completely independent site opening in the Experience Editor in Sitecore ready for action!

For this simple example all I’m going to show is how to capture someone clicking on the Register button (you would normally plan your events ahead however this shows its simplicity). If you click the “Capture Click Action” button, you should be able to select any HTML element on the page in order to assign a click action to it. For example, if you click on the “Register” button of the website you should see that Sitecore has identified that you have clicked on an Anchor tag and allow you to assign a “click action” to it:


You can navigate to the specific element in question if you need to and when you’re happy you can click on the “Add a new action” to further define your click action:


Once you’ve given your Click Action a name, you can assign any Goals or Analytic Attributes that you already have set up in Sitecore:


The rest of the UI should be familiar to you in terms of assigning goals/outcomes etc. When this is done ensure that your changes are saved and published – and that’s it 🙂

So, what happens when we browse our site?

When we browse our test site and click on the Register link, the Tracking Beacon Javascript will now call Sitecore to register the event. This can be evidenced in Telerik’s Fiddler tool:


Checking that this has worked.

You can verify this has worked in the usual places where you expect gather reports on Experience Analytics, for example, below shows the Page URL’s that have been tracked, which includes visits to my testsite:


Also, if you take a look in the Experience Profiler, you will also see the usual profile of the visits that have occured, including visitors of your external sites as shown in the detail of a specific visit below:


I hope this has given you a taste of the level of power you have with Federated Experience Manager. So far I’ve only scratched the surface of what’s achievable. I will be exploring other concepts of FXM in future posts.

Utilising simple Data Models in your Sublayouts

In this post I’m going to demonstrate a cleaner approach of  binding information to your Sublayouts by using simple Data Models.

Over the past few years I’ve been a huge advocate for utilising Datasources for absolutely everything. This is absolutely vital if you want to leverage Testability and Personlisation  – and never assume that a piece of content will never want to be personalised, you simply cannot tell what the future holds 🙂

There are cases though where data does not come directly from Datasources but is the product of a Search or some other form of acquisition. Generally this is bound to a Repeater and the fields a loosely typed in terms of pulling the data out:

<asp:Repeater id="rptTest" runat="server">
         <%# Eval("Name") %>
         <%# Eval("Author") %>
         <%# Eval("Intro Text") %>

This post will show you an alternative approach. Nowadays I use Data Models (or View Models?) to represent the information that needs to be rendered on the front end. This ensures that there is a strongly typed class (or data set) that contains what is needed (and no more). This also ensures that we have a consistent entity which can also interact with the rest of your code. In the instance of a Repeater, you can utilise the property ItemType in order to define which Data Model is bound to the repeater. Once this is done it makes it much simpler to pull the data out of the Datasource that is bound to the Repeater.

<asp:Repeater id="rptTest" runat="server" ItemType="SitecoreJim.Entities.Book">
        <%# Item.Title %>
        <%# Item.Author %>
        <%# Item.IntroText %>

Taking this one step further

Generally when I create Sublayouts I inherit from a base class called SublayoutBase which provides a good deal of generic functionality that we use everyday in our components. I have created a new variant of SublayoutBase in which you can pass in your Model type at the point you inherit from it:

public abstract class SublayoutBase<T> : SublayoutBase
    public T Model { get; set; }

    public abstract void CreateModel();

Also in the SublayoutBase there is an abstract method called CreateModel(). As its an abstract method, the intention is that when you inherit from SublayoutBase the implementation for this method is required –  the model is created in the place that understands how to create it. Now that we have everything in place, when we bind the Data Model to the Repeater and specify the ItemType, it makes it much easier to understand what information is bound to the Repeater and where the information is coming from.

Usage in the Page Editor

Using the methods above it is actually very easy to specify an example Datasource for our repeater specifically designed for the Page Editor. In the CreateModel() method it is a simple case of checking for the Page Editor and creating an example model for rendering

public override void CreateModel()
    List<Book> books = new List<Book>();

    if (Sitecore.Context.PageMode.IsPageEditor)
        books.Add(new Book()
            Title = "Black Diamond - How we started",
            Author = "Jamie Little",
            IntroText = "This is a great book"

        books.Add(new Book()
            Title = "From Rags to Riches, back to Rags and finally to Riches again",
            Author = "Stewie Mcnaughty",
            IntroText = "This is another great book";
        books.Add(new Book()
            Title = "Sense and Nonsensability",
            Author = "Fred Cringer";,
            IntroText = "How not to build stuff in Sitecore";
        // Populate Model using data

Remember, as this data comes from a different source it would not be content editable anyway, however it’s always good practice to show a representation of the component when the content editor inserts it into a placehder

The new SublayoutBase can be found here:


This inherits from a SublayoutBase class which you might also want to take a look at.

Anyway, any comments, feel free to leave them below

Sitecore SPEAK Gotcha #2 The StackOverflow exception when ID is part of your Entity

Using Sitecore Services Client is fun, but when the unexpected occurs it can be quite confusing to work out what is going on.

Last night was one of these cases; my code was constantly throwing a StackOverflow exception and I couldn’t understand why. There was no real indication and expected break points in code were not being hit.

After hours of scratching my head I decided to go back to basics with the Entities that I was using in part of my solution. It turns out that one of my Entities had an ID property which was causing the issue.

This is an example of the Entity I was using:

public class Template : Sitecore.Services.Core.Model.EntityIdentity
 public string itemId
 get { return base.Id; }
 set { base.Id = value; }

 public string TemplateName { get; set; }

 private ID _templateId = new ID();
 public ID TemplateID
 get { return _templateId; }
 _templateId = value;
 private List&amp;lt;string&amp;gt; _fields = new List&amp;lt;string&amp;gt;();
 public List&amp;lt;string&amp;gt; Fields
 get { return _fields; }
 set { _fields = value; }

The majority of the Entity above is simple enough, however having an ID property will cause a StackOverflow exception (so it seams). What was confusing though is that when invoking my service manually through a browser I did not experience any issues, only when invoking through a SPEAK application (through the fetchEntity function). Fiddler also confirmed that there was a difference between manual invocation and via the fetchEntity function.

What was going on? A SPEAK application (and related JavaScript) needs to be able to understand the information that is contained in your entities prior to using them (e.g. for client side validation). Currently, only the following simple types are supported:

  • string
  • bool
  • int
  • float
  • double
  • long
  • DateTime
  • Enum

(this is taken from the documentation at: https://sdn.sitecore.net/upload/sitecore7/75/developer%27s_guide_to_sitecore.services.client_sc75-a4.pdf)

What I’m going to take a look at is to see if there is a way of extending this to include other types (such as ID). For now, I’m simply using strings for ID’s until I can find an alternative solution 🙂

Sitecore SPEAK Gotcha #1 Populating itemId from your Entity

Just a quick post regarding a small gotcha I experienced with Sitecore Services Client.

Recently whilst setting up some services with SPEAK and Sitecore Services Client I experienced the following error when calling the “fetchEntities” from my SPEAK app to return information from my Template Service (a service that I’m creating to teach myself Sitecore Services Client and SPEAK – full blog post to appear soon regarding its creation)

Below is the JavaScript I’m using to retrieve my information:

var result = templateService.fetchEntity(selectedItem).execute().then(function (template) {
var title = template.Fields;

When inspecting this in the console of Chrome I was noticing the following:

404 (Entity does not exist)

This seemed a little strange, as the entity was present and my code was certainly creating and returning the data (debugging verified this). So why was this appearing?

Well, it appears that in your Entity, you need to populate “itemId” field in the base class (Sitecore.Services.Core.Model.EntityIdentity), otherwise the 404 error will occur.

Huge thanks to Mike Robbins @Sobek1985 for this 🙂

Applying Datasources to Sitecore Controls – The Easy Way

Following on from my post The Importance of Datasources I thought I’d show a neat little trick to ensure Datasources are bound to all Sitecore controls within any component/Sublayout that you create.

The approach I normally take to binding Datasources is to firstly inherit from a base class, and get the base class to expose a DataSourceItem property which returns the Datasource, or if one isn’t provided return the context item:

public Item DataSourceItem
        // Ensure at the very least Context Item is returned as a fallback
        Item returnItem = Sitecore.Context.Item;

        if (Sublayout != null)
            string dataSourceString = Sublayout.DataSource;

            if (!string.IsNullOrEmpty(dataSourceString))
                Item dbItem = Sitecore.Context.Database.GetItem(dataSourceString);

                if (dbItem != null)
                    returnItem = dbItem;

        return returnItem;

(You can get the full base class I use from GitHub at https://github.com/jamielittle/SitecoreToolbox/blob/master/Sitecore.Toolbox/Sublayout/SublayoutBase.cs)

Then in my Component, bind the DatasourceItem to each Sitecore control that is contained within the component:

scTextField1.Item = DatasourceItem;

scTextField2.Item = DatasourceItem;

scImage1.Item = DatasourceItem;

Doing this in every component is a bit of a chore, especially when there are many Sitecore controls contained within the component. So I’ve created a function that will do this for us:

        public void BindDatasourceToSitecoreControls()

        public void BindDatasourceToSitecoreControls(Item dataSource)
            if (Controls.Count &amp;gt; 0)
                BindControls(this.Controls, dataSource);

        private void BindControls(ControlCollection controls, Item dataSource)
            foreach (Control control in controls)
                if (control is FieldControl)
                    var fieldControl = control as FieldControl;
                    fieldControl.Item = dataSource;

                if (control is FieldRenderer)
                    var fieldRenderer = control as FieldRenderer;
                    fieldRenderer.Item = dataSource;

                if (control.Controls.Count &amp;gt; 0)
                    BindControls(control.Controls, dataSource);

The BindControls function can be used to recursively traverse through any control collection and when it encounters a control which is of type FieldRenderer or FieldControl (which covers most of the Sitecore controls) it will assign the Datasource that you pass into the function.

There are also two additional functions

  • BindDatasourceToSitecoreContrls() – This function will simply bind the Datasource that is available to any Sitecore controls it finds. This is part of my SublayoutBase.cs base class (as part of the Sitecore Toolbox) and inherits from System.Web.UI.Control so utilises the control collection that is available. You can simply call BindDatasourceToSitecoreControls() from you Page_Load() when inheriting from SubloayoutBase.
  • BingDatasourceToSitecoreControls(Item dataSource) – This function override can be used if a specific Item is used as a Datasource

The above code is included in the SitecoreToolbox on GitHub:


Hopes this helps when using Datasources.

Feel free to leave a comment if you have any questions 🙂

Sitecore Services Client – Creating a Custom Authorisation Filter

Recently I’ve been looking into the Sitecore Services Client with interest. Sitecore Services Client was developed to provide a consistent way of integrating Client applications with Sitecore. There are many applications for this. You may have a client application (A SPEAK application, or a Single Page Application on a website) or even other systems acting as a “client” which requires to call Sitecore for information.

Sitecore Services Client is very extensible. The aspect I’m going to touch upon in this post in authorisation of requests.

The scenario I’m going to use for this is “Only authorise Sitecore Services Client if the IP address of the caller is the same as Sitecore itself”. This scenario could be useful if you had a bunch of services that are only designed to be available from applications that execute on the same box.

To do this, I’m going to create a LoopbackAuthorisationFilter which is designed to only authorise requests that origniate from the same IP address.

Firstly, create your class, inheriting from AuthorisationFilterAttribute (in the System.Web.Http.Filters namespace) and override the OnAuthorization function.

public class LoopbackAuthorisationFilter : System.Web.Http.Filters.AuthorizationFilterAttribute
        public override void OnAuthorization(HttpActionContext actionContext)
            // Play nicely with the base class

            // If the request does not originate from this machine
            if (!System.Web.HttpContext.Current.Request.IsLocal)
                // Create an Unauthorised Access response
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, "Unauthorised Access");

What I’m doing here is ensuring that the original request originates from the local machine (with the same IP address). If the originating request is different, create a HTTP Unauthorized response back to the caller.

To wire this up in Sitecore Services Client, all you need to do is to add an entry into the section of the Sitecore.Services.Client.config file:

<filter>SSCActionFilter.LoopbackAuthorisationFilter, SSCActionFilter</filter>

It’s as simple as that 🙂

I have an example project on GitHub for you to try if you wish which will compile a DLL for you to use with your Sitecore Serice Client implementation:


Whilst this filter is simplistic, it is very easy to extend it to cater for any scenario you require, or even create your own Authorisation Filter.

Hope you have fun with this!

To find out more about the Sitecore Services Client, have a look at the Developers Guide to Sitecore.Services.Client here:


Indexing Multilist/Treelist fields and reading them back

Whilst in the process of ensuring Multilist/Treelist fields are being indexed and pulled back correctly, I came across a very useful blog post to assist in converting a list of GUID’s in the index back to an ID array


This works like a dream 🙂 Don’t forget to update your index config to reflect that you want to ensure that your field is being indexed (below is an example). The important part of this is ensuring “type” is set to “System.GUID”

<field fieldName="updated_specifications" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.GUID" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
<analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider" />

Also, you need to alter the definition of Droptree (or TreeListEx, or whatever field you are indexing) in <fieldTypes> section. By default the storageType is set to “NO”, change this to “YES”

Reindex your site with the TypeConverter and alterations to your index config and you should be good to go 🙂

Resetting Standard Fields for all Items of a given template

A very useful feature in Sitecore is the ability to reset fields on an item to their value as defined in Standard Values. This is great, but what if you want to reset fields on many items of the same template?

There doesn’t (yet) appear to be a feature in Sitecore to do this, so I wrote a simple function that performs this task.

Basically, you will need to perform a search to get all your items first, then pass in your list of items and a list of the field names you wish to reset on each item.

public void ResetFields(List<Item> items, List<string> fields)
    if (items.Any() && fields.Any())
        foreach (Item item in items)
           ResetFieldsOnItem(item, fields);

public void ResetFieldsOnItem(Item item, List<string> fields)
    if (item != null && fields.Any())
        using (new Sitecore.Security.Accounts.UserSwitcher(ELEVATED_USER_ACCOUNT, true))

            foreach (string field in fields)


I might create a SPEAK app so that when you click on a template, a button will appear to provide this functionality in the Ribbon – That would be a nice addition 🙂

Order of Sitecore Items utilising Sort Order and Created Date

A few times the ordering of objects when retrieving from Sitecore have caught me out. In a nutshell, the Sort Order field on an Item helps determines how Items are ordered, or an ordering “strategy” can be applied on the Item’s Parent to determine how the Items are sorted.

This is great, however it appears to be a little hit & miss as to whether the field will get populated if it is not understood what is happening. By default, if there is no sorting strategy specified by the Parent (by the Subitem Sorting field) then inserting an Item will leave the Sort Order field blank unless you rearrange the ordering of the items or change the Parent’s Subitem Sorting field (at which point you will see the Sort Order field populate by Sitecore). However, what if you copy, clone or move an item from another position in the tree? What if the Content Editor actually removes the Sort Order value from the field? You will end up in a position where some items have the Sort Order set and others do not (the field will be blank).

Also, if you are pulling items out of an Index (Lucene or Solr), the order of the results may not be the same as the order that they are in Sitecore. Thankfully this can be handled easily with a bit of code 🙂

As the Sort Order in Sitecore is a string field, you cannot simply perform an OrderBy() on the field as the ordering will be based on string ordering, not numeric ordering. Your results will be different.

This can cause some issues when querying items and you want to maintain the order that they appear in the CMS. The below code shows how I deal with that. Basically, I have a function delegate which handles the conversion form a string Sort Order to an integer value – if Sort Order is empty then it’s value will be 0.

private static Func<Item, int> GetSortOrderValue(List<Item> i)
 return x => string.IsNullOrEmpty(i[0][Sitecore.FieldIDs.Sortorder]) ? 0 : int.Parse(i[0][Sitecore.FieldIDs.Sortorder]);

Now we can utilise the above in a Linq statement so that we can order by Sort Order, and then order by Create Date (to cater for Items that have the same Sort Order)

private List<Item> OrderItems(List<Item> i)
    i = i.OrderBy(GetSortOrderValue(i)).ThenBy(y => y.Statistics.Created).ToList();
    return i;

Hope this makes sense 🙂

Sitecore Searching returns all languages from the Index

A quick one for today, something to be mindful of when performing a Sitecore Search against the indexes – what will be returned from your search will be a SearchResultItem for all languages that you have stored in your index, not just the language context.

You will need to perform the language filtering yourself 🙂
Quickest way of doing this is to either filter after the search has been done:

if (searchHit.Document.Language == Sitecore.Context.Language.Name)

  // your other code


.. or make it part of the search query

SearchResults<SearchResultItem> searchResults = context.GetQueryable<SearchResultItem>()
  .Filter(x => x.TemplateId == filterTemplace)
  .Filter(y => y.Language == Sitecore.Context.Language.Name)

Simple eh?