2

Is there any out-of-the-box way in Sitecore 9 for content authors to access the sitecore user profile information and put it INTO the content fields.

For example, in the experience editor - could a author add something to a string field like Welcome {Profile.FullName}! . I have seen some third party libraries for 8.x that seem to offer the ability to create your own tokens -- is this still the only applicable way? Will these work with the content items?

  • Hey Watson, can you explain a bit more your scenario. When an Author create a new page you want to have in the title something like Welcome {Author Name} ? – Vlad Iobagiu Mar 25 '18 at 4:29
  • Yes so - rather than create a hard-coded razor view adding in the current user's profile information - is there any way for the Content Authors to dynamically add that to what they are creating in the experience editor. So we might have a piece of content, containing a string field, which looks like "Welcome {token}" where token is replaced with the current logged in user's fullname (from their Sitecore User Profile). – Watson Mar 25 '18 at 11:33
2

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>
  • This is awesome -- thanks!! I will try it out then mark this as the answer. – Watson Mar 26 '18 at 13:10
  • Vlad -- will this work for the content delivery nodes as well? So the content author will create a field with Hi ${username} - in the content delivery node, when a user logs in - I want them to see their user name on the page. – Watson Mar 28 '18 at 17:31
  • it will work for content delivery. Can you write on sitecore slack your scenario, I guess you missunderstand how CM write content to CD using publishing... – Vlad Iobagiu Mar 28 '18 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.