Skip to content

Instantly share code, notes, and snippets.

@islaytitans
Created July 20, 2015 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save islaytitans/779d678dceabfadad619 to your computer and use it in GitHub Desktop.
Save islaytitans/779d678dceabfadad619 to your computer and use it in GitHub Desktop.
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