在处理程序中,我需要获取已修改的字段名称(不是字段值!)并将它们添加到日志中。我已尝试item.DisplayName
,item.Name
,但它是字段的值,而不是名称。
public void OnItemSavedHandled(object sender, EventArgs args)
{
Item temp = Event.ExtractParameter(args, 0) as Item;
Log.Info(string.Format(
"Item field {0} was modified at: {1}, by user:{2}",
temp.DisplayName,
DateTime.Now.ToString(), Sitecore.Context.User.Name),
this);
}
此代码添加以记录值,而不是字段的名称。如何获得名字?
答案 0 :(得分:5)
如果您要使用事件处理程序,则应考虑使用OnItemSaving
事件而不是OnItemSaved
事件。在保存项目之前,您可以循环遍历其字段,以使用单个字段的Field.IsFieldModified
属性确定字段值是否已更改。否则,如果您使用OnItemSaved
处理程序,则必须检索已保存项目的ItemChanges
对象,遍历ItemChanges
中的字段,然后检查{{1那里的财产。
以下是OnItemSaving事件处理程序的代码:
IsFieldModified
尽管如此,我实际上并不建议您将public void OnItemSaving(object sender, EventArgs args)
{
Item item = Event.ExtractParameter(args, 0) as Item;
if (item == null)
return;
item.Fields.ReadAll();
foreach (Field field in item.Fields)
{
if (!field.IsModified) //check if the field is modified
continue;
Log.Audit(string.Format("OnItemSaving - Item field {0} was modified at: {1}, by user: {2}", field.DisplayName, item.Statistics.Updated.ToString(CultureInfo.InvariantCulture), item.Statistics.UpdatedBy), this);
}
}
或OnItemSaved
事件用于您的目的。保存/保存事件由API引发,作为对项目执行的任何保存操作的一部分 - 无论是Sitecore还是Sitecore用户。因此,您可能会注意到,当您通常不希望它们出现时,会引发事件。例如,在发布过程中,执行保存操作,因此引发保存/保存事件。当发生意外的保存操作时,可能还有其他情况。
听起来你更愿意捕获用户保存事件?即当内容作者点击特定项目的“保存”按钮时?如果是这样,我建议您点按OnItemSaving
管道。仅当发生Sitecore UI保存事件时才触发此管道(例如,单击保存按钮,按Ctrl + S保存热键等等)
要使用Sitecore.Pipelines.Save
管道,您需要为管道创建处理器,然后将其添加到web.config文件中的Sitecore.Pipelines.Save
处理器(理想情况下,通过配置包含文件) 。以下是可用于管道处理器的代码:
/sitecore/process/saveUI
为了使此代码正常工作,在public class LogFieldChanges
{
public void Process(SaveArgs args)
{
foreach (var saveItem in args.Items) //loop through item(s) being saved
{
var item = Sitecore.Context.ContentDatabase.GetItem(saveItem.ID, saveItem.Language, saveItem.Version); //get the actual item being saved
if (item == null)
continue;
foreach (var saveField in saveItem.Fields) //loop through the fields being saved
{
var field = item.Fields[saveField.ID]; //retrieve the Field from the actual item
var isModified = saveField.OriginalValue != saveField.Value; //determine if the field has been modified, we only want to log modified fields
if (field == null || !isModified)
continue;
Log.Audit(string.Format("SaveUI - Item field {0} was modified at: {1}, by user: {2}", field.DisplayName, item.Statistics.Updated.ToString(CultureInfo.InvariantCulture), item.Statistics.UpdatedBy), this);
}
}
}
}
处理器之后插入自定义处理器非常重要。通过将其放在该处理器之后,您可以使用Sitecore.Pipelines.Save.Save
和SaveField.OriginalValue
属性来确定该字段是否已被修改。此外,通过将处理器放在SaveField.Value
处理器之后,您可以使用Sitecore.Pipelines.Save.Save
属性来确定项目的保存时间和人员。
Item.Statistics
答案 1 :(得分:3)
当你保存一个项目时,我想保存至少4个字段:你修改的项目,_ 更新, _更新和修订字段解决方案是下一个
public void OnItemSaved(object sender, EventArgs args)
{
SitecoreEventArgs eventArgs = args as SitecoreEventArgs;
Debug.Assert(eventArgs != null, "eventArgs != null");
//get the item who was changed
Item item = eventArgs.Parameters[0] as Item;
//get the fields that was modified
ItemChanges list = eventArgs.Parameters[1] as ItemChanges;
//parse the list of items and check if current field was modified
// I didn't find an solution without parsing list
foreach (Field field in list.Item.Fields)
if (list.IsFieldModified(field))
{
//get the field Name
string s = field.Name;
}
//now your code
}