1

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

4

2 回答 2

5

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

默认情况下,您只有以下标记:
$name:替换为已创建项目的名称
$parentname:替换为已创建项目的父项名称
$date:替换为当前日期
$time:替换为当前时间
$now:替换为当前日期和时间
$id:替换为已创建项目的 ID
$parentid:替换为已创建项目的父级 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());
    }
  }

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

这篇文章中,Alistair Deneys 也展示了一种不同的做法。

[编辑]
请注意,上面提供的(未经测试的)代码不适用于分支 - 仅适用于创建项目的“通常”方式。

于 2013-05-03T07:53:33.923 回答
2

如果您只想在前端网站上显示用户名,Sitecore 已经将创建项目的用户存储在__created字段中,您可以使用该字段并剥离域,即sitecore\

于 2013-05-03T09:01:34.830 回答