Is it possible to set the current user name to a field of an item in sitecore cms? -
for example, username in sitecore admin "borj", whenever create article want "borj" automatically fill author field of article create.
yes, possible, require customization.
by default have following tokens:
$name: replaced name of created item
$parentname: replaced name of parent created item
$date: replaced current date
$time: replaced current time
$now: replaced current date , time
$id: replaced id of created item
$parentid: replaced id of parent created item.
this post john west shows how fill field name of user that's creating item.
this code uses:
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()); } } if change setting mastervariablesreplacer own assembly , class, it'll pick on $user
in this post alistair deneys shows different way of doing well.
[edit]
please note (untested) code provided above not work branches - 'usual' way of creating items.
Comments
Post a Comment