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

是否可以将当前用户名设置为sitecore cms中项目的字段?,sitecore,Sitecore,例如,我在sitecore管理员中的用户名是“Borj”,每当我创建一篇文章时,我希望“Borj”自动填充我将创建的任何文章的作者字段 是的,这是可能的,但需要一些定制 默认情况下,您只有以下令牌: $name:替换为已创建项的名称 $parentname:替换为所创建项的父项的名称 $date:替换为当前日期 $time:替换为当前时间 $now:替换为当前日期和时间 $id:替换为已创建项的id $parentid:替换为所创建项的父项的id 向您精确显示如何使用创建项的用户的名称填充字段

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

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

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

向您精确显示如何使用创建项的用户的名称填充字段

这是他使用的代码:

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:SC.Data.MasterVariablesReplacer
{
公共重写字符串替换(字符串文本,SC.Data.Items.Item targetItem)
{
SC.Diagnostics.Assert.ArgumentNotNull(文本,“文本”);
SC.Diagnostics.Assert.ArgumentNotNull(targetItem,“targetItem”);
字符串结果=this.ReplaceValues(
文本
()=>targetItem.Name,
()=>targetItem.ID.ToString(),
()=>SC.Data.Items.ItemUtil.GetParentName(targetItem),
()=>targetItem.ParentID.ToString();
返回结果;
}
私有字符串替换值(
字符串文本,
Func defaultName,
Func defaultId,
Func defaultParentName,
Func defaultParentId)
{
如果((text.Length!=0)和&(text.IndexOf(“$”)>=0))
{
SC.Text.ReplacerContext context=this.GetContext();
if(上下文!=null)
{
foreach(context.Values中的KeyValuePair对)
{
text=text.Replace(pair.Key,pair.Value);
}
}
text=this.ReplaceWithDefault(文本“$name”、defaultName、上下文);
text=this.ReplaceWithDefault(文本“$id”、defaultId、上下文);
text=this.ReplaceWithDefault(文本“$parentid”,defaultParentId,上下文);
text=this.ReplaceWithDefault(文本“$parentname”、defaultParentName、上下文);
text=this.ReplaceWithDefault(文本“$date”,()=>SC.DateUtil.IsoNowDate,上下文);
text=this.ReplaceWithDefault(文本“$time”,()=>SC.DateUtil.IsoNowTime,上下文);
text=this.ReplaceWithDefault(text,“$now”,()=>SC.DateUtil.IsoNow,上下文);
text=this.ReplaceWithDefault(text,“$user”,()=>SC.Context.user.LocalName,Context);
}
返回文本;
}
私有字符串替换为默认值(
字符串文本,
字符串变量,
Func defaultValue,
SC.Text.ReplacerContext(上下文)
{
if((context!=null)&&context.Values.ContainsKey(变量))
{
返回文本;
}
if(text.IndexOf(variable,StringComparison.InvariantCulture)<0)
{
返回文本;
}
返回text.Replace(变量,defaultValue());
}
}
如果随后将设置
MasterVariablesReplacer
更改为您自己的程序集和类,它也将在
$user

在阿利斯泰尔,丹尼斯也展示了一种不同的方式

[编辑]

请注意,上面提供的(未经测试的)代码将不适用于分支机构-仅适用于创建项目的“常规”方式。

是的,这是可能的,但需要一些自定义

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

向您精确显示如何使用创建项的用户的名称填充字段

这是他使用的代码:

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:SC.Data.MasterVariablesReplacer
{
公共重写字符串替换(字符串文本,SC.Data.Items.Item targetItem)
{
SC.Diagnostics.Assert.ArgumentNotNull(文本,“文本”);
SC.Diagnostics.Assert.ArgumentNotNull(targetItem,“targetItem”);
字符串结果=this.ReplaceValues(
文本
()=>targetItem.Name,
()=>targetItem.ID.ToString(),
()=>SC.Data.Items.ItemUtil.GetParentName(targetItem),
()=>targetItem.ParentID.ToString();
返回结果;
}
私有字符串替换值(
字符串文本,
Func defaultName,
Func defaultId,
Func defaultParentName,
Func defaultParentId)
{
如果((text.Length!=0)和&(text.IndexOf(“$”)>=0))
{
SC.Text.ReplacerContext context=this.GetContext();
if(上下文!=null)
{
foreach(context.Values中的KeyValuePair对)
{
text=text.Replace(pair.Key,pair.Value);
}
}
text=this.ReplaceWithDefault(文本“$name”、defaultName、上下文);
text=this.ReplaceWithDefault(文本“$id”、defaultId、上下文);
text=this.ReplaceWithDefault(文本“$parentid”,defaultParentId,上下文);
text=this.ReplaceWithDefault(文本“$parentname”、defaultParentName、上下文);
text=this.ReplaceWithDefault(文本“$date”,()=>SC.DateUtil.IsoNowDate,上下文);
text=this.ReplaceWithDefault(文本“$time”,()=>SC.DateUtil.IsoNowTime,上下文);
text=this.ReplaceWithDefault(text,“$now”,()=>SC.DateUtil.IsoNow,上下文);