2

I'm working on a script to modify rendering parameters. I noticed that I seem to only be able to modify the Shared Layout and not the Final Layout. I can retrieve the Final Layout field and work based off of it, but in order to apply the changes, item[Sitecore.FieldIDs.LayoutField] = layoutDefinition.ToXml(); works but item[Sitecore.FieldIDs.FinalLayoutField] = layoutDefinition.ToXml();

I am concerned about modifying the Shared Layout having unintended consequences from modifying the shared layout instead of the Final Layout, as language versions may have different renderings and I don't want to overwrite language-specific renderings.

This updates my layout (both Shared and Final):

var layoutField = new LayoutField(item.Fields[Sitecore.FieldIDs.FinalLayoutField]);
var layoutDefinition = LayoutDefinition.Parse(layoutField.Value);
var deviceDefinition = layoutDefinition.GetDevice(defaultDeviceId);

<stuff to modify the renderings>

item.Editing.BeginEdit();
item[Sitecore.FieldIDs.LayoutField] = layoutDefinition.ToXml();
item.Editing.EndEdit();

This does not appear to update anything:

var layoutField = new LayoutField(item.Fields[Sitecore.FieldIDs.FinalLayoutField]);
var layoutDefinition = LayoutDefinition.Parse(layoutField.Value);
var deviceDefinition = layoutDefinition.GetDevice(defaultDeviceId);

<stuff to modify the renderings>

item.Editing.BeginEdit();
item[Sitecore.FieldIDs.FinalLayoutField] = layoutDefinition.ToXml();
item.Editing.EndEdit();

EDIT: I am not just trying to modify rendering parameter field values but also need to be able to move a rendering's position and to change its placeholder.

2 Answers 2

2

Based on this blogpost, you can do it in the following way:

item.Editing.BeginEdit();
layoutField.Value = layoutDefinition.ToXml();
item.Editing.BeginEdit();
1
  • That seems to work! Not sure why item[Sitecore.FieldIDs.FinalLayoutField] = layoutDefinition.ToXml(); didn't Commented Dec 24, 2019 at 14:18
0

If you Sitecore version is 8 or later then you Probably need to use SetFieldValue([NotNull] Field field, [NotNull] string value) method to change __Renderings or __Final Renderings.

Something like below,

item.Editing.BeginEdit();
LayoutField.SetFieldValue(item.Fields[Sitecore.FieldIDs.FinalLayoutField], "Updated Value");
item.Editing.EndEdit();

Refer Sitecore Document here also note this particular comment from Sitecore document mentioned in the link,

The SetFieldValue method does not always save the complete layout value to the field. If possible, it saves only the XML patch to the selected field.

1
  • It appears this would only work for setting rendering parameter field values and not for changing the placeholder or the position in the layout Commented Dec 24, 2019 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.