ASP.NET Web API 2 – Enable Sitecore Context Session State

How do I bring that big thing back into the context?

How do I bring that big thing back into the context?

ASP.NET Web API 2 is designed as a stateless Web API framework, therefore, the visitor session context is not automatically created on each web request. Normally, for a Sitecore solution, it is necessary to enable Sitecore Context session-dependent features/data in some or even most of Web API calls context.

The Solution

One of the options is to create Web API Route which would automatically enable session state for all Web API calls that are made via this route. To be clear, we are basically looking for enabling a regular ASP.NET session state in the context of ASP.NET Web API 2 calls.

The solution option which we consider in this specific case has the following implementation approach:

  1. Register a new route using ASP.NET Web API 2 – a dedicated route for the session-context-enabled Web API calls handling.
  2. Retrieve the new route’s object using a regular ASP.NET and enrich it with a request handler that creates a regular ASP.NET session state context.

The Code

(mehr …)

ASP.NET Web API 2 – Sitecore HTML Cache Action Filter with ETag

You do not need to write this code every time ...

You do not need to write this code every time …

Nowadays, almost every Sitecore solution needs a custom Web API endpoint implementation. One of the common web API uses cases when a front-end component needs to be dynamically rendered at the client-side (eg a browser page). However, there could be some challenging factors of the back-end implementation:

  1. The Web API response data needs to be presented in JSON format.
  2. The Web API response data is calculated based on multiple Sitecore items.
    – The data evaluation is expensive in the performance perspective so it has to be cached.
    – The data cache needs to be invalidated when the corresponding Sitecore items are changed.
  3. The data to be transferred over the network only the data has been changed, otherwise, the client-side cached version of it will be used.

The Solution

To serve the data in JSON format via Web API endpoint, it is possible to use ASP.NET Web API 2. Therefore, no significant challenges here.

To cache the data Sitecore HTML Cache can be used. Sitecore HTML Cache invalidates automatically by Sitecore (normally, on Publish). In this case, the Controller Action would need to produce the data and let Sitecore HTML Cache store it. However, it would be good to remove the cache storage responsibility from the Controller Action. In addition, it would be necessary to avoid Controller Action execution if the data already exists in the cache. Luckily,  ASP.NET Web API 2 Action Filter attributes exist that can help to do this. The Action Filter attribute has two handlers:

  • OnActionExecuting – allows accessing the current request context, including the request parameter object (s) and terminate the request with a custom response without going ahead with the Controller Action execution.
  • OnActionExecuted – allows accessing the response result in the cache.

To address the redundant data transfer between the server and the client, HTTP ETag mechanism can be used which allows producing 304 Not Modified HTTP response. Again, A SP.NET Web API 2 can help to solve this. 

HTTP ETag mechanism, including Sitecore HTML Cache, can be applied in a generic way to any controller using Action Filter attributes.

The Code

(mehr …)

How to Create a Rule in Sitecore to automatically fill an empty field with existing content

Using the Sitecore Rule engine for solving a use case for automatically filling a field in Sitecore, when saving a page item: an empty Page Description field should inherit the content of an existing Introduction field. Weiterlesen

Store Item „Published date“ and „Published by“ information on each Sitecore item

Sitecore Publish Statistics for Published by and Published date - Teaser

Did you ever miss that Sitecore doesn’t keep track of who published which item(s) at what date? Well, be not sad no longer! In this blog post you’ll find a neat solution to store the „Published date“ and „Published by [user]“ information on all Sitecore items – so it’s finally possible to blame the right person who published too many items once again 😉 Weiterlesen

How to automatically enable/disable a Sitecore CMS user account at a specific date-time

Sitecore auto enable disable CMS user account - Teaser

When you need a Sitecore CMS user account to be only valid during a specific time period – or automatically disabled at a certain point in time, we present you a neat solution to achieve this here. Weiterlesen

Boosting international targeted SEO for your website using Sitecore

Global multilanguage and multicountry websites with Sitecore - International targeted SEO

In this article we do a deep-dive focusing on how to achieve great international targeted SEO by using the Sitecore Experience Platform‘s multilanguage capabilities. Weiterlesen

Basic concepts & principles for building global multi-language and multi-country websites using Sitecore

Global multilanguage and multicountry websites with Sitecore - Teaser

Conceptual thoughts and recommendations for building globally focused websites based on the Sitecore Experience Platform with multiple languages and country cultures. Consider this a non conclusive compilation of learnings and recommendations from our experience building such web platforms since many years. (Part 1 of a series) Weiterlesen

How to resolve an Application Cache System.NullReferenceException in Sitecore.Speak.Client

Sitecore Log Analyzer Icon

The situation One of our clients reported recently, that the content managers started to be affected by a situation, where they were… not able to fully load the Sitecore Experience Editor anymore (stuck with the spinning loading-icon) and also weren’t … Weiterlesen

Add Export to File Functionality in Sitecore’s Search Options

This blog post is based on the Article Add Export to File Functionality in Sitecore’s Search Options by Dean Mark Soriano.

To follow this guidance as well as possible it is required that you have already completed the steps from this blog post:


Now we want to export the result list into an excel file.

(mehr …)