1

I am writing a item:created event. I need to update one of these values, however the .Fields collection comes up empty

  • 3
    can you add here your code? – Vlad Iobagiu Jul 11 '17 at 6:04
2

Try to use item:added event like this:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>
    <events>
      <event name="item:added">
        <handler type="MyAssembly.MyNamespace.ItemEventHandler, MyAssembly" method="OnItemAdded"/>
      </event>
    </events>
  </sitecore>
</configuration>

And then in your code:

public class ItemEventHandler
{
    protected void OnItemAdded(object sender, System.EventArgs args)
    {
        if (args == null)
            return;

        var item = Sitecore.Events.Event.ExtractParameter(args, 0) as Sitecore.Data.Items.Item;
        if (item == null)
            return;

        var field = item.Fields["TestField"];
        if (field != null)
        {
            item.Editing.BeginEdit();
            field.Value += "ItemEventHandler";
            item.Editing.EndEdit();
        }
    }
}
  • Its strange, all this time I was using the item:created event which was not returning the fields, however the item:saved event seems to be returning the fields, any ideas why? – tmp dev Jul 12 '17 at 3:46
  • I only know that Sitecore itself does not use item:created. It uses item:added for newly created items. And other events like item:saved or item:moved for other events. Remember that item:saved is called every time item is saved, not only when item is created. – Marek Musielak Jul 12 '17 at 7:16
  • yeah, so I have two choices, choice 1: use item:created: cannot do much here since I do not have access to the fields. choice 2: use item:saved. I can update the fields here, but not the ideal place. – tmp dev Jul 12 '17 at 22:04
  • The odd thing is that if you look way back in their release notes, item:added was supposed to be deprecated in favor of item:created. Personally, I try not to use either event and instead use the <addFromTemplate> item provider pipeline – Zachary Kniebel Jul 12 '17 at 22:26
1

Hope the below details about the events give you a better understanding.

  1. item:added and item:created • These events seem similar, but Sitecore only fires item:added when a user creates an item through the UI. • It fires item:created when code creates items through APIs. • Use item:added if you only need to trap manual item additions.

  2. item:saved and item:saving • Sitecore fires item:saved in either case(item:added and item:created), Using item:saved lets me put all the logic in a single handler, which fires after create, update, rename, move and potentially other events. • item:saving event before committing the change, while it raises the item:saved event afterwards. An advantage of using the item:saving event as opposed to the item:saved event is that you can access the values from the item before the save, and the new values, and can prevent the user from saving their changes.

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.