Automatically refresh page in Sitecore Experience Editor after updating a component

“EditFrames in the Experience Editor is a feature that you can use to give users front-end access to fields that are not directly accessible, either because they are not visible or they are of a type that are not directly front-end editable.”

When editing a component using EditFrames or the Editor Button, the component datasource does not refresh. To do this, you have to save the page first, followed by reloading the page with the latest changes, which is cumbersome for content editors.

Changing this behavior can be changed by overriding Pipelines, one of Sitecore’s essential integration concepts.

Sitecore.Shell.Applications.ContentManager.ReturnFieldEditorValues.SetValues

public void Process(ReturnFieldEditorValuesArgs args)
{
    Assert.ArgumentNotNull(args, nameof(args));

    args.Options.SaveItem = true;
    foreach (FieldInfo fieldInfo in args.FieldInfo.Values)
    {
        Control subControl = Context.ClientPage.FindSubControl(fieldInfo.ID);
        if (subControl == null) continue;

        var value = subControl is IContentField
            ? StringUtil.GetString((subControl as IContentField).GetValue())
            : StringUtil.GetString(ReflectionUtil.GetProperty(subControl, "Value"));

        if (value == "__#!$No value$!#__") continue;

        string type = fieldInfo.Type.ToLowerInvariant();
        if (type == "rich text" && type == "html") continue;

        value = value.TrimEnd(' ');

        foreach (FieldDescriptor fieldDescriptor in args.Options.Fields)
        {
            if (fieldDescriptor.FieldID != fieldInfo.FieldID) continue;

            ItemUri itemUri = new ItemUri(fieldInfo.ItemID, fieldInfo.Language, fieldInfo.Version,
                Factory.GetDatabase(fieldDescriptor.ItemUri.DatabaseName));
            if (fieldDescriptor.ItemUri == itemUri)
                fieldDescriptor.Value = value;
            Item item = Factory.GetDatabase(fieldDescriptor.ItemUri.DatabaseName)
                .GetItem(itemUri.ItemID);

            if (item == null) continue;

            item.Editing.BeginEdit();
            item.Fields[fieldInfo.FieldID.ToString()].Value = value;
            item.Editing.EndEdit();
        }
    }
}

Sitecore.Shell.Applications.ContentManager.ReturnFieldEditorValues.ReturnAndClose

public void Process(ReturnFieldEditorValuesArgs args)
{
    Assert.ArgumentNotNull(args, nameof(args));

    SheerResponse.SetDialogValue(args.Options.ToUrlHandle().ToHandleString());
    SheerResponse.SetModified(true);
    SheerResponse.CloseWindow();

    SheerResponse.Eval("window.top.location.reload();");
}

Create a Sitecore Patch file

Create a new Patch file inside the /App_Config/Include folder. Below an example of a Sitecore patch file including a custom pipelines is added.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <processors>
        <uiReturnFieldEditorValues>
            <processor type="YourNameSpace.SetValues, YourAssembly"
            patch:instead="processor[@type='Sitecore.Shell.Applications.ContentManager.ReturnFieldEditorValues.SetValues']" />
            <processor type="YourNameSpace.ReturnAndClose, YourAssembly"
            patch:instead="processor[@type='Sitecore.Shell.Applications.ContentManager.ReturnFieldEditorValues.ReturnAndClose']" />
        </uiReturnFieldEditorValues>
    </processors>
  </sitecore>
</configuration>