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>
item:saving
event? community.sitecore.net/technical_blogs/b/sitecorejohn_blog/…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 theitem:saved
event instead.