3

I want to share authority to read content with another system. When contents are published to the public site, we create processing to output the data to the database.

Is there a way to catch only if you include a change in the __Security field?

It was possible to catch the item updated by the item:saved event. I am in trouble because I do not know which field was changed.

2

You can use item:saving event.

You can compare the original and the new item there, e.g.:

private static List<string> FindDifferences(Item newItem, Item originalItem)
{
    newItem.Fields.ReadAll();

    IEnumerable<string> fieldNames = newItem.Fields.Select(f => f.Name);

    return fieldNames
        .Where(fieldName => newItem[fieldName] != originalItem[fieldName])
        .ToList();
}

protected void OnItemSaving(object sender, EventArgs args)
{
  Item newItem = Event.ExtractParameter(args, 0) as Item;

  if (newItem == null || newItem.Database.DataManager.GetWorkflowInfo(newItem) == null)
  {
    return;
  }

  Item originalItem = 
    newItem.Database.GetItem(newItem.ID, newItem.Language, newItem.Version);

  var differences = FindDifferences(newItem, originalItem);

  // your code here
}

and register

<sitecore>
  <events>
    <event name="item:saving">
      <handler type="My.Assembly.Namespace.MyEventHandler,
        My.Assembly" method="OnItemSaving" />
    </event>
  </events>
</sitecore>

Please be aware that in some cases saving of the item can be intercepted so not necessarily your changes will be saved.

Long time ago I wrote a blog post about tracking history of item changes https://www.cognifide.com/our-blogs/sitecore/tracking-history-of-item-fields-changes-in-sitecore - I copied code from there.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.