Try our conversational search powered by Generative AI!

Area: Optimizely CMS
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

How it works

Episerver supports context-sensitive client components. Context could be the current page that the user is working with. Because components are loosely coupled, the context service object is responsible for handling the current context. To get hold of the current context you could simply do this:

var contextService = epi.dependency.resolve("");
var currentContext = contextService.currentContext;

The context service on the client interacts with the server to resolve context for the selected page/block/entity. The actual properties on the context object depends on the type of context being displayed but a few properties are available for all context:

uri The key uniquely identifying the entity in context.
previewUrl The URL on which the entity can be displayed in the editing UI.
data The values of the entity in context.
type The type of data for this context. This information is derived from the uri.
id The id of the data for this context. This information is derived from the uri.

Context on the server implements the IClientContext interface and is serialized to the client.

Example: epi.cms.contentdata Context


    "name":"Alloy Track",
The ContentDataContext class is serialized to produce this client context.

Using _ContextMixin to access context in components

To simplify interaction with the context you can implement a mix-in that provides access to the context from a component.

define("epi-cms/component/PageVersions", [
], function (_ContextMixin) {
    return dojo.declare("epi.cms.component.PageVersions", [_ContextMixin], {
        constructor: function(){
            dojo.when(this.getCurrentContext(), function(context){
                console.log("we have context: ", context);

The _ContextMixin extends the class with a the following methods:

getCurrentContext() Returns an available context or a deferred object that resolves to the context when it is available.
currentContext The currently loaded context. This may be null.
contextChanged(context, callerData) Overridable method called when the context changes. Components should check that the context is not null and may look at context.type to determine whether they can work with a given context. contextChangeFailed(previousContext, callerData) Overridable method called when loading the context fails for any reason.

Changing the current context

If you need to change the current context, publish a message to the /epi/shell/context/request channel with parameters for the new context as the data. A URI parameter property is required to determine which type of context to load. The following example changes the current context to an Episerver CMS ContentData object:

var pageLink = "5_135"; //Replace with code to determine the pageLink for the page you want to change to..
var contextParameters = { uri: 'epi.cms.contentdata:///' + pageLink };
dojo.publish("/epi/shell/context/request", [contextParameters]);

Listening to changes to the current context using subscription

Without using _ContextMixin, you can subscribe to context changes using the dojo pub/sub infrastructure. If you want to react to a context change, subscribe to the /epi/shell/context/changed message:

startup: function () {
    this.subscribe("/epi/shell/context/changed", this._contextChanged);
_contextChanged: function (newContext) {
    if (!newContext) {
    //Do something with newContext.

Implementing a context resolver on the server

To provide context for a certain type of entities you can implement the following context resolver interfaces.

  • Resolve a known reference using URI.
  • Query context on a specific URL.

The following example shows a context resolver that performs both these tasks:

/// Resolves URIs and URLs to files for the shell context service.
public class CmsFileContextResolver : IUriContextResolver, IUrlContextResolver
    private VirtualPathProvider _vpp;

    public CmsFileContextResolver(VirtualPathProvider vpp)
        SortOrder = 123;
        _vpp = vpp;

    /// The name of the model the context contextualizes.
    public string Name
        get { return "epi.cms.unifiedfile"; }

    /// Resolves a page link in URI format.
    public bool TryResolveUri(Uri uri, out IClientContext instance)
        instance = ResolvePath("~" + uri.LocalPath);
        return instance != null;

    /// Tries to resolve an url to page context.
    public bool TryResolveUrl(Uri url, out IClientContext instance)
        instance = ResolvePath(url.LocalPath);
        return instance != null;

    private IClientContext ResolvePath(string path)
        var file = _vpp.GetFile(path) as UnifiedFile;
        if (file == null)
            return null;
        return new Models.UnifiedFileContext
            Uri = new Uri("epi.file.unifiedfile://" + VirtualPathUtilityEx.ToAppRelative(file.VirtualPath).TrimStart('~')),
            Data = new
                // ... more unifiedfile-specific data
            PreviewUrl = file.VirtualPath

    /// Sort order among peer url resolvers.
    public int SortOrder { get; set; }

Querying for a new context by URL

Context resolvers that implement the IUrlContextResolver interface are queried when you load a URL in the preview frame. Publish a message to programmatically change the context to the context associated with a URL, as shown in the following example. 

// queries the context service for the context belonging to a URL
var contextParameters = { url: someUnknownUrl.toString() };
dojo.publish("/epi/shell/context/request", [contextParameters, { sender: this }]);

ContextHistory and navigating back to previous context

[New in Episerver CMS UI 9.9.0]

When an editor navigates from one context to another or the same, Episerver keeps the history of every step through the  ContextHistory and BackContextHistory classes on the client side (both are internal to Episerver).

As a developer, you can clear the current context (and close the related view), and go back to the previous context. This is usually the case when you want to close or cancel a view related to the current context and go back.

For example, in the Project Overview, there is a close button. Clicking this button means clearing all Project context history and going back to the previous context.

You can achieve the same thing with the contextNavigation object on client side. This is a static object on the client side, exposing a method "back". It clears the history of the current context and then goes to the previous context.  


define("epi-cms/contextNavigation", function (contextNavigation) {
    return dojo.declare("epi.cms.component.MyCustomeView", {
        clicked: function(){
Do you find this information helpful? Please log in to provide feedback.

Last updated: Aug 15, 2016

Recommended reading