如何在创建自定义事件处理程序时获取sitecore中项目的字段名称

Use*_*ser 2 c# sitecore sitecore6

在处理程序中,我需要获取已修改的字段名称(不是字段值!)并将它们添加到日志中.我试过了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);
}
Run Code Online (Sandbox Code Playgroud)

此代码添加以记录值,而不是字段的名称.怎么得到一个名字?

Ada*_*ber 5

如果您要使用事件处理程序,则应考虑使用OnItemSaving事件而不是OnItemSaved事件.在保存项目之前,您可以遍历其字段以使用Field.IsFieldModified单个字段的属性确定字段值是否已更改.否则,如果使用OnItemSaved处理程序,则必须检索ItemChanges已保存项目的对象,遍历其中的字段ItemChanges,然后检查其中的IsFieldModified属性.

这是OnItemSaving事件处理程序的代码:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

尽管如此,我实际上并不建议将这些OnItemSavedOnItemSaving事件用于您的目的.保存/保存事件由API引发,作为对项目执行的任何保存操作的一部分 - 无论是Sitecore还是Sitecore用户.因此,您可能会注意到,当您通常不希望它们出现时,会引发事件.例如,在发布过程中,执行保存操作,因此引发保存/保存事件.当发生意外的保存操作时,可能还有其他情况.

听起来你宁愿捕获用户保存事件?即当内容作者点击特定项目的"保存"按钮时?如果是这样,我会建议进入Sitecore.Pipelines.Save管道.仅当Sitecore UI保存事件发生时才触发此管道(例如,单击保存按钮,按Ctrl + S热键保存等...)

要使用Sitecore.Pipelines.Save管道,您需要为管道创建处理器,然后将其添加到/sitecore/process/saveUIweb.config文件中的处理器(理想情况下,通过配置包含文件).以下是可用于管道处理器的代码:

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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

为了使此代码正常工作,在Sitecore.Pipelines.Save.Save处理器之后插入自定义处理器非常重要.通过将其放在该处理器之后,您可以使用SaveField.OriginalValueSaveField.Value属性来确定该字段是否已被修改.此外,通过将处理器放在处理器之后Sitecore.Pipelines.Save.Save,您可以使用Item.Statistics属性来确定项目的保存时间和人员.

<sitecore>
    <processors>
        <saveUI>            
            .
            .
            <processor mode="on" type="Sitecore.Pipelines.Save.Save, Sitecore.Kernel" />
            <!-- insert your processor after the Sitecore.Pipelines.Save.Save processor -->
            <processor mode="on" type="Sitecore.Extensions.Pipelines.Save.LogFieldChanges, Sitecore.Extensions"/>
            .
            .
         </saveUI>
    </processors>
</sitecore>
Run Code Online (Sandbox Code Playgroud)

  • 非常好的答案.只有注释:使用包含文件而不是更改web.config:http://www.sitecore.net/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2011/05/All-About-Web-配置,包括档案,用最Sitecore的-ASPNET,CMS.aspx (2认同)