Skip to content

Instantly share code, notes, and snippets.

@islaytitans

islaytitans/ComputeUpdate.cs Secret

Created Jul 20, 2015
Embed
What would you like to do?
Hackathon - Programmatically update a Sitecore Item
private bool ComputeUpdate(Item item, ItemModel itemModel)
{
bool success = false;
var fieldsToRevert = itemModel.Fields.Where(x => x.RevertToStandardValue);
using (new Sitecore.SecurityModel.SecurityDisabler())
{
try
{
item.Editing.BeginEdit();
foreach (var sField in fieldsToRevert)
{
var field = item.Fields.FirstOrDefault(x => x.ID.ToString().Trim().ToLower() == sField.Id.Trim().ToLower());
if (field == null)
{
Log.Error("Failed to revert field to standard values, field was not found on item", this);
continue;
}
field.Value = field.GetStandardValue();
if (field.Value.Contains("$"))
{
Sitecore.Data.MasterVariablesReplacer replacer = Sitecore.Configuration.Factory.GetMasterVariablesReplacer();
if (replacer != null)
replacer.ReplaceItem(item);
}
}
success = true;
}
catch (Exception ex)
{
Log.Error(ex.Message, ex, this);
throw ex;
}
finally
{
item.Editing.EndEdit();
}
}
return success;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment