Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're going to use the event handlers, you should consider using the <code>OnItemSaving</code> event instead of the <code>OnItemSaved</code> event. Before an item is saved, you can loop through it's fields to determine if a field value has changed using the <code>Field.IsFieldModified</code> property of an individual field. Otherwise, if you use the <code>OnItemSaved</code> handler, you'll have to retrieve the <code>ItemChanges</code> object for the item that was saved, loop through fields in the <code>ItemChanges</code>, and check the <code>IsFieldModified</code> property there.</p> <p>Here is code for the OnItemSaving event handler:</p> <pre><code>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); } } </code></pre> <p>With all of that said, I wouldn't actually recommend using the <code>OnItemSaved</code> or <code>OnItemSaving</code> events for your purposes. The saved/saving events are raised by the API as a part of ANY save operation performed on the item - whether by Sitecore or by a Sitecore user. Therefore, you may notice that the events are being raised when you wouldn't normally expect them to be. For instance, during the publish process, a save operation is executed and therefore the saved/saving events are raised. There may be other instances when unintended save operations occur as well.</p> <p>It sounds like you'd rather just capture user save events? i.e. when a content author clicks the 'Save' button for a specific item? If so, I would recommend tapping into the <code>Sitecore.Pipelines.Save</code> pipeline. This pipeline is triggered only when a Sitecore UI save event occurs (e.g. Save button click, Ctrl+S hot key save, etc...)</p> <p>To use the <code>Sitecore.Pipelines.Save</code> pipeline, you will need to create a processor for the pipeline, then add it to the <code>/sitecore/process/saveUI</code> processor in your web.config file (ideally, via a config include file). Here is code you could use for the pipeline processor:</p> <pre><code>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); } } } } </code></pre> <p>In order for this code to work, it's important that your custom processor be inserted AFTER the <code>Sitecore.Pipelines.Save.Save</code> processor. By placing it after that processor, you're able to use the <code>SaveField.OriginalValue</code> and <code>SaveField.Value</code> properties to determine if the field has been modified. Also, by placing your processor after the <code>Sitecore.Pipelines.Save.Save</code> processor, you're able to use the <code>Item.Statistics</code> properties to determine when and by whom the item was saved.</p> <pre><code>&lt;sitecore&gt; &lt;processors&gt; &lt;saveUI&gt; . . &lt;processor mode="on" type="Sitecore.Pipelines.Save.Save, Sitecore.Kernel" /&gt; &lt;!-- insert your processor after the Sitecore.Pipelines.Save.Save processor --&gt; &lt;processor mode="on" type="Sitecore.Extensions.Pipelines.Save.LogFieldChanges, Sitecore.Extensions"/&gt; . . &lt;/saveUI&gt; &lt;/processors&gt; &lt;/sitecore&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload