public void ShouldWorkWithLayoutDeltas() { ID templateId = ID.NewID; string templateLayout = @"<r xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <d id=""{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}"" l=""{4CA7478E-6184-4890-9072-1156DB468A1B}"" /> <d id=""{EF7176AE-2502-401A-96C0-1E9918A982F7}"" l=""{4CA7478E-6184-4890-9072-1156DB468A1B}"" /> <d id=""{5A6E7DC3-987F-4E74-AF78-AC0E544975F2}"" l=""{4CA7478E-6184-4890-9072-1156DB468A1B}"" /> </r>"; string itemDelta = @"<r xmlns:p=""p"" xmlns:s=""s"" p:p=""1""> <d id=""{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}""> <r uid=""{BC2FDEAE-A971-420B-A874-BA5C767C42FE}"" s:id=""{B5BFA387-74C8-416B-98AF-01C9230C24B2}"" s:ph=""Score Content Main"" /> </d> </r>"; string merged = XmlDeltas.ApplyDelta(templateLayout, itemDelta); using (var db = new Db() { new DbTemplate("main", templateId) { { FieldIDs.LayoutField, templateLayout } }, new DbItem("page", ID.NewID, templateId), new DbItem("page2", ID.NewID, templateId) { new DbField(FieldIDs.LayoutField) { Value = itemDelta } } }) { Item item = db.GetItem("/sitecore/content/page"); #pragma warning disable 618 Assert.Equal(templateLayout, StandardValuesManager.Provider.GetStandardValue(item.Fields[FieldIDs.LayoutField])); #pragma warning restore 618 Assert.Equal(templateLayout, LayoutField.GetFieldValue(item.Fields[FieldIDs.LayoutField])); // standard values Assert.Equal(templateLayout, item[FieldIDs.LayoutField]); var item2 = db.GetItem("/sitecore/content/page2"); Assert.Equal(templateLayout, item2.Fields[FieldIDs.LayoutField].GetStandardValue()); // just checking Assert.True(XmlPatchUtils.IsXmlPatch(itemDelta)); Assert.Equal(merged, LayoutField.GetFieldValue(item2.Fields[FieldIDs.LayoutField])); } }
private static string GetLayoutFieldValue(Field field) { var value = field.GetValue(true, true); var func = XmlDeltas.WithEmptyValue("<r />"); if (string.IsNullOrEmpty(value)) { return(value); } if (!XmlPatchUtils.IsXmlPatch(value)) { return(value); } return(XmlDeltas.ApplyDelta(func(field), value)); }
/// <summary> /// /// </summary> /// <param name="template"></param> /// <param name="delta"></param> /// <param name="language"></param> /// <param name="version"></param> /// <param name="database"></param> private void ApplyDeltaToStandardValues(Template template, string delta, Language language, Sitecore.Data.Version version, Database database) { if (Sitecore.Context.User.IsAdministrator) { if (template != null) { if (!Sitecore.Data.ID.IsNullOrEmpty(template.StandardValueHolderId)) { Item item = ItemManager.GetItem(template.StandardValueHolderId, language, version, database, SecurityCheck.Disable); if (item != null) { Field field = item.Fields[FieldIDs.LayoutField]; if (!field.ContainsStandardValue) { try { string newFieldValue = XmlDeltas.ApplyDelta(field.Value, delta); if (newFieldValue != field.Value) { item.Editing.BeginEdit(); LayoutField.SetFieldValue(field, newFieldValue); item.Editing.EndEdit(); } } catch (Exception ex) { Sitecore.Diagnostics.Log.Error(ex.Message, this); } } } } } } }
private void ProcessField(Field field) { string initialValue; var fieldValue = string.Empty; if (field.ID != Sitecore.FieldIDs.LayoutField && field.ID != Sitecore.FieldIDs.FinalLayoutField) { initialValue = GetInitialFieldValue(field); } else { //Special Case. //Full Starndard + Delta. fieldValue = field.GetValue(true, true); //Patched Value initialValue = GetLayoutFieldValue(field); } if (string.IsNullOrEmpty(initialValue)) { return; } //Get Keys : Ids and Paths var keys = GetKeys(initialValue); //Checks keys needs to be ignored otherwise equivalent item exist, if yes prepare list of equal items other wise add in ignore. GetReplaceableList(keys); //Replace with equivaent item list. if (!_result.OrAny()) { return; } var value = new StringBuilder(initialValue); foreach (var r in _result) { value = value.Replace(r.Value.Source.ID.Guid.ToString("D").ToUpper(), r.Value.Dest.ID.Guid.ToString("D").ToUpper()); value = value.Replace(r.Value.Source.ID.Guid.ToString("D").ToLower(), r.Value.Dest.ID.Guid.ToString("D").ToLower()); value = value.Replace(r.Value.Source.ID.Guid.ToString("N").ToUpper(), r.Value.Dest.ID.Guid.ToString("N").ToUpper()); value = value.Replace(r.Value.Source.ID.Guid.ToString("N").ToLower(), r.Value.Dest.ID.Guid.ToString("N").ToLower()); value = value.Replace(r.Value.Source.Paths.Path, r.Value.Dest.Paths.Path, true); value = value.Replace(r.Value.Source.Paths.Path.ToLower(), r.Value.Dest.Paths.Path.ToLower(), true); if (!r.Value.Source.Paths.IsContentItem) { continue; } value.Replace(r.Value.Source.Paths.ContentPath, r.Value.Dest.Paths.ContentPath); value.Replace(r.Value.Source.Paths.ContentPath.ToLower(), r.Value.Dest.Paths.ContentPath.ToLower()); } //Special care taken to handler Standard Value and Delta of layout , thus to maintain inheritance otherwise it will break inhertiance // and any change to template will not reflect in derived pages. if (field.ID == Sitecore.FieldIDs.LayoutField || field.ID == Sitecore.FieldIDs.FinalLayoutField) { //Difference of old value and new updated value, thus inheritance remain in place. //Try to get new patch. initialValue = XmlDeltas.ApplyDelta(fieldValue, initialValue); } UpdateFieldValue(field, initialValue, value); }