2013-07-16 2 views
2

처리기에서 수정 된 필드 이름 (필드 값 아님!)을 가져 와서 로그에 추가해야합니다. 나는 item.DisplayName, item.Name을 시도했지만 이름이 아닌 필드의 값입니다.사용자 지정 이벤트 처리기를 만들 때 sitecore에서 항목의 필드 이름을 얻는 방법

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); 
} 

이 코드는 필드 이름이 아닌 값을 기록하도록 추가합니다. 이름을 얻는 방법?

답변

5

옆에있는 항목을 수정, _ 에 의해 업데이트, _Updated 및 개정 필드 을 이벤트 처리기를 사용하는 경우 OnItemSaved 이벤트 대신 OnItemSaving 이벤트를 사용해야합니다. 항목을 저장하기 전에 해당 필드를 반복하여 개별 필드의 Field.IsFieldModified 속성을 사용하여 필드 값이 변경되었는지 확인할 수 있습니다. 그렇지 않으면 OnItemSaved 처리기를 사용하는 경우 저장된 항목에 대해 ItemChanges 개체를 가져와 ItemChanges의 입력란을 반복하고 IsFieldModified 속성을 확인해야합니다. 즉, 실제로 당신의 목적을 위해 OnItemSaved 또는 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); 
    } 
} 

: 여기

는 OnItemSaving 이벤트 처리기에 대한 코드입니다. 저장/저장 이벤트는 Sitecore 또는 Sitecore 사용자에 관계없이 항목에서 수행 된 모든 저장 작업의 일부로 API에 의해 발생합니다. 따라서 이벤트가 정상적으로 발생하지 않을 것으로 예상 될 때 이벤트가 발생하고 있음을 알 수 있습니다. 예를 들어 게시 프로세스 중에 저장 작업이 실행되고 따라서 저장/저장 이벤트가 발생합니다. 의도하지 않은 저장 조작이 발생할 경우 다른 인스턴스가있을 수 있습니다.

사용자 저장 이벤트를 캡처하는 것 같습니다. 즉 콘텐츠 작성자가 특정 항목에 대해 '저장'버튼을 클릭하면 어떻게됩니까? 그렇다면 Sitecore.Pipelines.Save 파이프 라인을 활용하는 것이 좋습니다. 이 파이프 라인은 Sitecore UI 저장 이벤트가 발생할 때만 트리거됩니다 (예 : 저장 버튼 클릭, Ctrl + S 단축키 저장 등)

Sitecore.Pipelines.Save 파이프 라인을 사용하려면 파이프 라인 용 프로세서를 만들어야합니다 그런 다음 web.config 파일의 /sitecore/process/saveUI 프로세서 (이상적으로는 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); 
      } 
     } 
    } 
} 

위해이 코드가 작동하려면, 그것은 사용자 정의 프로세서는 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> 
+2

의 Field 클래스에 관해서는 아주 좋은 대답입니다. 주석 만 : web.config를 변경하는 대신 포함 파일을 사용하십시오. http://www.sitecore.net/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2011/05/All-About-Web- config-Include-Files-with-the-Sitecore-ASPNET-CMS.aspx – Trayek

+0

Sitecore 7을 사용하고 있는데 필드의 속성이 같지 않습니다. 특히 field.DisplayName. – DanielV

+0

@Adam Weber : foreach (saveItem.Fields의 var saveField) 행에서 var의 형식이 아닌 File을 넣어 개체의 모든 속성을 가져 오는 것이 좋습니다. – DanielV

3

당신은 항목이 내가 저장하는 것 같아요 4 개 필드 minimun 저장할 때 :이 솔루션은 당신이가는 경우 하나

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 
    } 
+0

감사합니다. 그리고이 코드의 필드 란 무엇입니까? – User

+0

모든 항목 필드를 구문 분석하고 수정되었는지 확인하십시오. 개인적으로 나는 해결책을 좋아하지 않지만 다른 해결책을 찾지 못했다. 그러나 나는 그것에 대해 많은 시간을 검색하지 않았다 ... –

+0

@ 'Field' 필드'에 대한 사용자의 응답 '-> * Item은 많은 Fields *의 콜렉션이고 Field는 우리가 Item과 같은 클래스 유형입니다. 자세한 내용은 [Sitecore SDN with Field Types] (http://sdn.sitecore.net/Articles/Media/Referencing%20Binary%20Items/Field%20Types.aspx) –