Insights

< Back to Insights

Here's How to Control Sitecore Item Statistic Values When Editing an Item Programmatically

If you are modifying an item programmatically, you're probably familiar with the Sitecore.Data.Items.ItemEditing API class.

The Sitecore Item can be put in Editing mode by using the .Editing.BeginEdit() method off the item (e.g. sitecoreItem.Editing.BeginEdit();) You'll need to remember to turn off editing mode with .Editing.EndEdit();

Or, you may be taking advantage of this using statement: using (new EditContext(sitecoreItem)) { --- field changes here ---- }.

Whichever way you choose, if you use the statements above without additional parameters, the Modified and Modified By fields will be auto-populated by Sitecore, even if you set them while modifying the item.

You can turn off this functionality by setting the 'updateStatistics' optional parameter to false. Another parameter must also be set: 'silent' ... when set to true, it won't run the Item:Saved events (including saving to the Publish queue ... this is handy if running a batch job).

Here is how to implement this using each method above (the first bool parameter is for 'updateStatistics'; the second for 'silent': sitecoreItem.Editing.EndEdit(false, true);

OR

using (new EditContext(sitecoreItem, false,true)) { --- field changes here ---- }.

Ready to take a deeper dive? Tweet @VerndaleTweets using #TechTipTuesday or drop us a line here to learn how to take the next step!