Sunday 11 June 2017

Sitecore: Add Custom Standard Values Tokens


Sitecore provides below seven out-of-the-box standard values tokens

    $name: The name for the new item entered by the user
    $id: The ID of the new item
    $parentid: The ID of the parent of the new item
    $parentname: The name of the parent of the new item
    $date: The system date in yyyyMMdd format
    $time: The system time in HHmmss format
    $now: The system date and time in yyyyMMddTHHmmss format

Although in most cases you may fulfill your requirements using above tokens, but sometimes, you may need to create additional tokens.

For example, showing user full name who created the item.

This can simply be done by extending Sitecore's master variable replacer class,  Sitecore.Data.MasterVariablesReplacer


using Sitecore;
using Sitecore.Collections;
using Sitecore.Text;
using System;
using System.Collections.Generic;
 
namespace Sitecore.BaseKit.Foundation.ContentEditor
{
    public class MasterVariablesReplacer : Sitecore.Data.MasterVariablesReplacer
    {
      protected override string ReplaceValues(string text, 
        Func<string> defaultName, 
        Func<string> defaultId, 
        Func<string> defaultParentName, 
        Func<string> defaultParentId)
        {
            bool flag = text.Length != 0 && text.IndexOf('$') >= 0;
            if (flag)
            {
                ReplacerContext context = this.GetContext();
                bool flag2 = context != null;
                if (flag2)
                {
                    foreach (KeyValuePair<string, string> current in context.Values)
                    {
                        text = text.Replace(current.Key, current.Value);
                    }
                }
                text = this.ReplaceWithDefault(text, "$name", defaultName, context);
                text = this.ReplaceWithDefault(text, "$id", defaultId, context);
                text = this.ReplaceWithDefault(text, "$parentid", defaultParentId, context);
                text = this.ReplaceWithDefault(text, "$parentname", defaultParentName, context);
                text = this.ReplaceWithDefault(text, "$date", () => DateUtil.IsoNowDate, context);
                text = this.ReplaceWithDefault(text, "$time", () => DateUtil.IsoNowTime, context);    
                text = this.ReplaceWithDefault(text, "$now", () => DateUtil.IsoNow, context);
                text = this.ReplaceWithDefault(text, "$username", () => Context.User.Profile.FullName, context);            
            }
            return text;
        } 
    }
}
 

As, you can see I have added $username token and replaced its value with Sitecore.Context.User.Profile.FullName, the same way you can create any custom token and replace its value as per your requirements.

Remember, tokens get replaced at the time of Item creation only, once your items are created and then you add your token in Standard Values, it will not get replaced in existing items, instead tokens will inherit as it is, ex. $name, $username etc. in already created items.

Once you are done with code part, next step is to create a patch configuration file to override existing MasterVariablesReplacer class.

Create a config file and name it Sitecore.Foundation.ContentEditor.MasterVariablesReplacer.config, add below patch lines and save it.


<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <settings>
      <setting name="MasterVariablesReplacer">
        <patch:attribute name="value">Sitecore.BaseKit.Foundation.ContentEditor.MasterVariablesReplacer,Sitecore.BaseKit.Foundation.ContentEditor</patch:attribute>
      </setting>
    </settings>
  </sitecore>
</configuration>

Now, final step, add this patch file in App_Config/Include folder of your site root, and you are done!!

 

So, How to use it?...


1. Set token in template _Standard Values



2. Create Item from that template, you will see token has been replaced by the user full name who created the item



Any suggestion/feedback, reach out to me.
Happy coding :)

No comments:

Post a Comment