In Sitecore 9 you need to customize a bit to replace customtokens with User Name.
In next example you will replace $user with the name of the user
First off all you need to extend MasterVariablesReplacer:
public class CustomMasterVariablesReplacer: MasterVariablesReplacer
{
protected override string ReplaceValues(string text, Func<string> defaultName, Func<string> defaultId, Func<string> defaultParentName, Func<string> defaultParentId)
{
if (text.Length == 0 || text.IndexOf('$') < 0)
return text;
ReplacerContext context = this.GetContext();
if (context != null)
{
foreach (KeyValuePair<string, string> keyValuePair in (SafeDictionary<string, string>)context.Values)
text = text.Replace(keyValuePair.Key, keyValuePair.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", (Func<string>)(() => DateUtil.IsoNowDate), context);
text = this.ReplaceWithDefault(text, "$time", (Func<string>)(() => DateUtil.IsoNowTime), context);
text = this.ReplaceWithDefault(text, "$now", (Func<string>)(() => DateUtil.IsoNow), context);
text = this.ReplaceWithDefault(text, "$user", (Func<string>)(() => Sitecore.Context.User.Name), context);
return text;
}
}
You need to register new master variable replacer in a config file :
<configuration xmlns:x="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore role:require="Standalone or ContentManagement">
<settings>
<setting name="MasterVariablesReplacer" value="YourNamespace.CustomMasterVariablesReplacer,YourDll"/>
</settings>
</sitecore>
</configuration>