/// <summary> /// When you change a template field from versioned to shared or unversioned (or vice versa), the conversion of the values /// in existing items with that field is NOT automatic. This method causes the requisite field updates (moving the values between db tables) /// </summary> protected void UpdateFieldSharingIfNeeded(IItemData serializedItemData, Item targetItem) { // This is what Sitecore's internal deserializer does instead. Will investigate if this is a better option. Also depends on which Sitecore version introduced this. //if (EventDisabler.IsActive) // ReflectionUtil.CallMethod(targetItem.Database.Engines.TemplateEngine, "HandleItemSaved", new object[] {targetItem, (ItemChanges) ReflectionUtil.CallMethod(targetItem, "GetFullChanges"), false}); //return; Assert.ArgumentNotNull(serializedItemData, nameof(serializedItemData)); Assert.ArgumentNotNull(targetItem, nameof(targetItem)); if (serializedItemData.TemplateId != TemplateIDs.TemplateField.Guid) { return; } var shared = serializedItemData.SharedFields.FirstOrDefault(field => field.FieldId == TemplateFieldIDs.Shared.Guid); var unversioned = serializedItemData.SharedFields.FirstOrDefault(field => field.FieldId == TemplateFieldIDs.Unversioned.Guid); TemplateFieldSharing sharedness = TemplateFieldSharing.None; if (shared != null && shared.Value.Equals("1")) { sharedness = TemplateFieldSharing.Shared; } else if (unversioned != null && unversioned.Value.Equals("1")) { sharedness = TemplateFieldSharing.Unversioned; } var templateField = TemplateManager.GetTemplateField(targetItem.ID, targetItem.Parent.Parent.ID, targetItem.Database); if (templateField == null) { // BOMB WIG CacheManager.ClearAllCaches(); targetItem.Database.Engines.TemplateEngine.Reset(); templateField = TemplateManager.GetTemplateField(targetItem.ID, targetItem.Parent.Parent.ID, targetItem.Database); } Assert.IsNotNull(templateField, $"Unable to find template field {targetItem.ID} in {targetItem.Database.Name}, even after resetting the engine."); TemplateFieldSharing templateSharedness = TemplateFieldSharing.None; if (templateField.IsShared) { templateSharedness = TemplateFieldSharing.Shared; } else if (templateField.IsUnversioned) { templateSharedness = TemplateFieldSharing.Unversioned; } if (templateSharedness == sharedness) { return; } TemplateManager.ChangeFieldSharing(templateField, sharedness, targetItem.Database); }
/// <summary> /// When you change a template field from versioned to shared or unversioned (or vice versa), the conversion of the values /// in existing items with that field is NOT automatic. This method causes the requisite field updates (moving the values between db tables) /// </summary> protected void UpdateFieldSharingIfNeeded(IItemData serializedItemData, Item targetItem) { Assert.ArgumentNotNull(serializedItemData, nameof(serializedItemData)); Assert.ArgumentNotNull(targetItem, nameof(targetItem)); if (serializedItemData.TemplateId != TemplateIDs.TemplateField.Guid) { return; } var shared = serializedItemData.SharedFields.FirstOrDefault(field => field.FieldId == TemplateFieldIDs.Shared.Guid); var unversioned = serializedItemData.SharedFields.FirstOrDefault(field => field.FieldId == TemplateFieldIDs.Unversioned.Guid); TemplateFieldSharing sharedness = TemplateFieldSharing.None; if (shared != null && shared.Value.Equals("1")) { sharedness = TemplateFieldSharing.Shared; } else if (unversioned != null && unversioned.Value.Equals("1")) { sharedness = TemplateFieldSharing.Unversioned; } var templateField = TemplateManager.GetTemplateField(targetItem.ID, targetItem.Parent.Parent.ID, targetItem.Database); if (templateField == null) { // BOMB WIG CacheManager.ClearAllCaches(); targetItem.Database.Engines.TemplateEngine.Reset(); templateField = TemplateManager.GetTemplateField(targetItem.ID, targetItem.Parent.Parent.ID, targetItem.Database); } Assert.IsNotNull(templateField, $"Unable to find template field {targetItem.ID} in {targetItem.Database.Name}, even after resetting the engine."); TemplateFieldSharing templateSharedness = TemplateFieldSharing.None; if (templateField.IsShared) { templateSharedness = TemplateFieldSharing.Shared; } else if (templateField.IsUnversioned) { templateSharedness = TemplateFieldSharing.Unversioned; } if (templateSharedness == sharedness) { return; } TemplateManager.ChangeFieldSharing(templateField, sharedness, targetItem.Database); }
/// <summary> /// When you change a template field from versioned to shared or unversioned (or vice versa), the conversion of the values /// in existing items with that field is NOT automatic. This method causes the requisite field updates (moving the values between db tables) /// </summary> protected void UpdateFieldSharingIfNeeded(IItemData serializedItemData, Item targetItem) { if (serializedItemData.TemplateId != TemplateIDs.TemplateField.Guid) { return; } var shared = serializedItemData.SharedFields.FirstOrDefault(field => field.FieldId == TemplateFieldIDs.Shared.Guid); var unversioned = serializedItemData.SharedFields.FirstOrDefault(field => field.FieldId == TemplateFieldIDs.Unversioned.Guid); TemplateFieldSharing sharedness = TemplateFieldSharing.None; if (shared != null && shared.Value.Equals("1")) { sharedness = TemplateFieldSharing.Shared; } else if (unversioned != null && unversioned.Value.Equals("1")) { sharedness = TemplateFieldSharing.Unversioned; } var templateField = TemplateManager.GetTemplateField(targetItem.ID, targetItem.Parent.Parent.ID, targetItem.Database); TemplateFieldSharing templateSharedness = TemplateFieldSharing.None; if (templateField.IsShared) { templateSharedness = TemplateFieldSharing.Shared; } else if (templateField.IsUnversioned) { templateSharedness = TemplateFieldSharing.Unversioned; } if (templateSharedness == sharedness) { return; } TemplateManager.ChangeFieldSharing(templateField, sharedness, targetItem.Database); }