Tag: Pipeline

Sitecore Custom Tokens

Out of the box, Sitecore comes with the following expand standard values tokens:

  • $name – Name of the item
  • $date – Current server date
  • $time – Current server time
  • $now – Current server date time
  • $id – Item ID
  • $parentid – Item’s Parent ID
  • $parentname – Item’s Parent Name

Sometimes the already existing tokens are not enough. There are cases in which the Content Editors won`t be able to access the value that needs to be populated (ex. – webservice call) or it will be hard for them to do it (ex. – the value of the item varies on many other items). In the current example, the $next token will be implemented. It will automatically replace the token with the current siblings’ count plus one  – this token is usually used in cases where there is a requirement for auto-incrementing sort order of items.

When it comes down to creating your own custom tokens, there are basically two options to choose from:

  1. Extend the MasterVariablesReplacer class
  2. Extend the ExpandInitialFieldValueProcessor class

The first option involves extending and overriding default functionality of the class, that provides the replacement mechanism for all the standard Sitecore tokens. This approach is explained in John West’s blogpost on how to add custom standard value tokens to Sitecore. It should be noted, that extending from the MasterVariablesReplacer class requires some method overriding. Be aware, that since the class is not really meant for extending in the first place you’ll have to duplicate a great amount of the private members into your own custom implementation.

The other option is the inheritance from the ExpandInitialFieldValueProcessor class, which is my preferred way to create replacement mechanisms for custom tokens in Sitecore. Using the ExpandInitialFieldValueProcessor is really easy, as it only requires you to extend from the base class and implement the void Process(ExpandInitialFieldValueArgs args) method:

namespace AndreyVinda.Processors
{
    using Sitecore.Pipelines.ExpandInitialFieldValue;

    public class TokenNextProcessor : ExpandInitialFieldValueProcessor
    {
        public override void Process(ExpandInitialFieldValueArgs args)
        {
            if (args.SourceField.Value.Contains("$next"))
            {
                if (args.TargetItem != null && args.TargetItem.Parent != null && args.TargetItem.Children != null)
                {
                    args.Result = args.Result.Replace("$next", args.TargetItem.Parent.Children.Count.ToString());
                }
                else
                {
                    args.Result = args.Result.Replace("$next", "0");
                }
            }
        }
    }
}

The code is pretty straightforward. It checks if the value of the field contains the $next token. If it does it checks if the parent is not null and the parent has children. If the parent is not null and there are children the value is set to the children`s count (the processor is executed after the child is added so the index will be 1 based), otherwise it sets the value to 0.

Once the token replacer is implemented, all that is left to do is to patch in the processor into the Sitecore pipeline config section for the expandInitialFieldValue pipeline:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <expandInitialFieldValue>
        <processor type="AndreyVinda.Processors.TokenNextProcessor , AndreyVinda.Processors" patch:after="processor[@type='type=Sitecore.Pipelines.ExpandInitialFieldValue.ReplaceVariables, Sitecore.Kernel']"/>
      </expandInitialFieldValue>
    </pipelines>
  </sitecore>
</configuration>

Enjoy!

Sitecore: 404 without 302.

Sitecore: 404 without 302.

This post shows how to handle a request for a page which is not found, how to avoid having the request being redirected with a 302 status code before a 404.

How Sitecore handle NotFoundItem

Sitecore lets you specify a page to use for ‘ItemNotFound’ errors in web.config, and it’s always good practice to have a pretty ‘page not found’ page:

<setting name="ItemNotFoundUrl" value="/404" />

When Sitecore can’t find the requested item, it will redirect the user to the page that is configured in the setting ItemNotFoundUrl.

However, during the redirecting the response returns an HTTP status code 302 (Moved Temporarily). This is sad, as search engines don’t understand that the page actually hasn’t been found.

For search engine optimization, you should return the HTTP 404 status code for any invalid URL by setting the Status property of the current System.Web.HttpResponse

Continue reading “Sitecore: 404 without 302.”

Avoid displaying pages without a language version

The Sitecore CMS allows you to create multiple versions of the pages for each of the languages supported by your site. Still, sometimes you may want to have particular pages translated into to a certain subset of the languages, e.g. if you have French content that should be available for end users in just France and Canada. The problem is that when you try to access the page in a language which does not have a supported version, you won’t get a 404 error, but be displayed an empty page.

Continue reading “Avoid displaying pages without a language version”