是否可以将当前用户名设置为sitecore cms中项目的字段?

Bor*_*orj 1 sitecore

例如,我在sitecore管理员中的用户名是"Borj",每当我创建一篇文章时,我希望"Borj"自动填充我要创建的任何文章的作者字段.

Tra*_*yek 5

是的,这是可能的,但需要一些定制.

默认情况下,您只有以下标记::
$name被替换为创建项目的名称:替换为创建项目
$parentname的父项名称
$date:替换为当前日期
$time:替换为当前时间
$now:替换为当前日期和时间
$id:替换为已创建项目
$parentid的ID:替换为创建项目的父项ID.

John West的这篇文章向您展示了如何使用创建项目的用户的名称填充字段.

这是他使用的代码:

public class MasterVariablesReplacer : SC.Data.MasterVariablesReplacer
  {
    public override string Replace(string text, SC.Data.Items.Item targetItem)
    {
      SC.Diagnostics.Assert.ArgumentNotNull(text, "text");
      SC.Diagnostics.Assert.ArgumentNotNull(targetItem, "targetItem");
      string result = this.ReplaceValues(
        text,
        () => targetItem.Name,
        () => targetItem.ID.ToString(),
        () => SC.Data.Items.ItemUtil.GetParentName(targetItem),
        () => targetItem.ParentID.ToString());
      return result;
    }

    private string ReplaceValues(
      string text,
      Func<string> defaultName,
      Func<string> defaultId,
      Func<string> defaultParentName,
      Func<string> defaultParentId)
    {
      if ((text.Length != 0) && (text.IndexOf('$') >= 0))
      {
        SC.Text.ReplacerContext context = this.GetContext();

        if (context != null)
        {
          foreach (KeyValuePair<string, string> pair in context.Values)
          {
            text = text.Replace(pair.Key, pair.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", () => SC.DateUtil.IsoNowDate, context);
        text = this.ReplaceWithDefault(text, "$time", () => SC.DateUtil.IsoNowTime, context);
        text = this.ReplaceWithDefault(text, "$now", () => SC.DateUtil.IsoNow, context);
        text = this.ReplaceWithDefault(text, "$user", () => SC.Context.User.LocalName, context);
      }

      return text;
    }

    private string ReplaceWithDefault(
      string text, 
      string variable, 
      Func<string> defaultValue, 
      SC.Text.ReplacerContext context)
    {
      if ((context != null) && context.Values.ContainsKey(variable))
      {
        return text;
      }

      if (text.IndexOf(variable, StringComparison.InvariantCulture) < 0)
      {
        return text;
      }

      return text.Replace(variable, defaultValue());
    }
  }
Run Code Online (Sandbox Code Playgroud)

如果您随后将设置更改MasterVariablesReplacer为您自己的程序集和类,它也会接受$user

这篇文章中, Alistair Deneys也展示了一种不同的方式.

[edit]
请注意,上面提供的(未经测试的)代码不适用于分支 - 只需使用"通常"的方式创建项目.