Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建自定义事件处理程序时如何获取sitecore中项的字段名_C#_Sitecore_Sitecore6 - Fatal编程技术网

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

C# 创建自定义事件处理程序时如何获取sitecore中项的字段名,c#,sitecore,sitecore6,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 fie

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

此代码添加到日志中的是一个值,而不是字段的名称。如何获取名称?

保存项目时,我猜至少有4个字段是保存:您修改的项目、\u更新人、\u更新和修订字段 解决方案是下一个

    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
    }

如果你要使用事件处理程序,你应该考虑使用<代码> OnItMeMeave事件而不是<代码> OnItMeMeave事件。在保存项目之前,您可以使用单个字段的

field.IsFieldModified
属性循环查看其字段,以确定字段值是否已更改。否则,如果使用
OnItemSaved
处理程序,则必须检索已保存项的
ItemChanges
对象,遍历
ItemChanges
中的字段,并检查其中的
IsFieldModified
属性

以下是OnItemSaveing事件处理程序的代码:

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);
    }
}
综上所述,我并不建议出于您的目的使用
onitemsave
OnItemSaving
事件。保存/保存事件由API引发,作为对项目执行的任何保存操作的一部分(无论是由Sitecore还是由Sitecore用户)。因此,您可能会注意到,事件是在您通常不期望的情况下引发的。例如,在发布过程中,执行保存操作,因此引发保存/保存事件。可能还会出现其他非预期的保存操作

听起来您更希望捕获用户保存事件?i、 e.当内容作者单击特定项目的“保存”按钮时?如果是这样,我建议点击
Sitecore.Pipelines.Save
管道。仅当发生Sitecore UI保存事件时(例如,单击保存按钮、Ctrl+S热键保存等),才会触发此管道

要使用
Sitecore.Pipelines.Save
管道,您需要为管道创建一个处理器,然后将其添加到web.config文件中的
/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
属性来确定项目的保存时间和保存人

<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>

.
.
.
.

解析所有项目字段并检查是否已修改。个人我不喜欢这个解决方案,但我没有找到其他解决方案,但我没有花太多时间搜索它…@用户回答什么是“
Field
Field”->
Item
是许多字段的集合,
Field
是一种与
Item
相同的类。您将在Sitecore SDNVery中找到关于字段类的更多详细信息。唯一备注:使用include files而不是更改web.config:我使用的是Sitecore 7,字段的属性不同,特别是:field.DisplayName。@亚当·韦伯:在:foreach(saveItem.Fields中的var saveField)行中,最好将文件作为类型,而不是var,为了获取对象的所有属性。@DanielV,DisplayName是“Sitecore.Data.Fields.Field”类型的属性,该类型是“Field”变量的类型。您是指“saveField”变量吗?该变量的类型为“Sitecore.Pipelines.Save.SaveArgs.SaveField”,它没有DisplayName属性。我不建议将“saveField”强制转换为“File”——我认为这甚至不是有效的强制转换。也许您想将“field”变量强制转换为“FIle”类型?即使如此,也不建议这样做,除非您知道正在处理的字段实际上是一个“文件”字段。