0

I want to access all the data of an item when I click Save button in Sitecore.

Suppose I have an ITEM A. If I click Save button in Sitecore, I would like to store all the data in the fields to a text file on the file system.

How can I achieve that? Thanks for the help.

  • item:saving does not make changes to an item by itself. It simply fires an event which you can capture and do what you want with the item that is saving. As I say in my answer below, however, I would recommend using the item:saved event instead. – Dan Sinclair Mar 20 '19 at 15:44
  • @DanSinclair Okay thanks. I will try this method. – PSS Mar 20 '19 at 16:05
3

I would recommend creating an item:saved event handler to take the data and save the information you need.

Note: the item:saved event is different from the item:saving event and I believe preferable in this case because the item:saving event happens while the editor waits for the save command to complete.

Create a custom event handler

public class WriteFileItemSavedEventHandler
{
  public void OnItemSaved(object sender, EventArgs args)
  {
    // Extract the item from the event Arguments
    Item savedItem = Event.ExtractParameter(args, 0) as Item;

    // If we don't have an item or we're not saving in the master DB, ignore this save
    if (savedItem == null || !"master".Equals(savedItem.Database?.Name, StringComparison.OrdinalIgnoreCase))
      return;

    if (!savedItem .TemplateID.Equals(new ID("{YOUR-TEMPLATE-ID}")))
      return;

    // Get the fields you care about and write out the file here
  }
}

Create a config patch file

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <events>
      <event name="item:saved">
        <handler type="Custom.Events.WriteFileItemSavedEventHandler, Custom" method="OnItemSaved"/>
      </event>
    </events>
  </sitecore>
</configuration>

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.