#Sitecore ‘s SaveUI Pipeline One Way it Can Save You from the Cache

Recently I had a bug fix that required fixing a custom tab page in the content editor. The custom page had a save button, but users were also clicking on the content editor save button. Well unfortunately that undid the previous change. The reason why was even though the changes were saved to the database the old values were still being cached.

So after doing some research on maybe refreshing, clearing cache etc… the best way to fix this issue turned out to be was adding a custom pipeline step on the SaveUI pipeline. It was a little a trial and error at first, but I was able to accomplish what I needed to.

First thing is you want to do is come up with a name for your new class and create a config file that will hook into the SaveUI pipeline. Now you have some options here. If you look below the SaveUI has a series of steps that happen in order for an item to save. You just need to find the one you want your step to go before or after.

<saveUI>
<processor mode=”on” type=”Sitecore.Pipelines.Save.BeforeSaveEvent, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.ParseXml, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.CheckItemLock, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.CheckRevision, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.Validators, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.ValidateFields, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.HasWritePermission, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.NewVersion, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.TightenRelativeImageLinks, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.ConvertToXHtml, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.CheckLock, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.Lock, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.CheckBaseTemplateFieldChange, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.CheckTemplateFieldChange, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.ConvertLayoutField, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.CheckLinks, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.Save, Sitecore.Kernel” />
<processor mode=”off” type=”Sitecore.Pipelines.Save.RenderingHack, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.Unlock, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.WorkflowSaveCommand, Sitecore.Kernel” />
<processor mode=”on” type=”Sitecore.Pipelines.Save.PostAction, Sitecore.Kernel” />
</saveUI>

I created a config file and placed it in the app_config\include folder of the website.

<configuration xmlns:patch=”http://www.sitecore.net/xmlconfig/”&gt;
<sitecore>
<processors>
<saveUI>
<processor patch:before=”processor[@type=’Sitecore.Pipelines.Save.CheckRevision, Sitecore.Kernel’]” type=”MySite.Feature.Website.Pipelines.ConfirmChanges,MySite.Feature.Website” />
</saveUI>
</processors>
</sitecore>
</configuration>

The code for the pipeline was simple. Basically I would check for what fields changed and made sure what was saved in the database fields would update the fields in the cache. That way when the save happens the SaveUI pipeline will process the changes and not overwrite anything.

public class ConfirmChanges
{
public void Process(SC.Pipelines.Save.SaveArgs args)
{
Assert.ArgumentNotNull(args, “args”);

if (!args.HasSheerUI)
{
return;
}

Assert.IsNotNull(SC.Context.ContentDatabase, “Sitecore.Context.ContentDatbabase”);
string message = string.Empty;

// for each of the items the user attempts to save
foreach (SC.Pipelines.Save.SaveArgs.SaveItem uiItem in args.Items)
{
// retrieve from the database the same item containing the old values
SC.Data.Items.Item dbItem = SC.Context.ContentDatabase.GetItem(uiItem.ID, uiItem.Language, uiItem.Version);
Assert.IsNotNull(dbItem, “dbItem”);

// for each of the fields in that item (not just updated fields)
if (UsesCustomTab(dbItem))
{
foreach (SC.Pipelines.Save.SaveArgs.SaveField uiField in uiItem.Fields)
{
if (uiField.Value == dbItem[uiField.ID])
{
continue;
}
string title = string.IsNullOrEmpty(dbItem.Fields[uiField.ID].Title)
? dbItem.Fields[uiField.ID].DisplayName
: dbItem.Fields[uiField.ID].Title;
if (title == “FieldName1” || title == “FieldName2”)
{
//if values are the same don’t do anything exit pipeline.
if (!string.IsNullOrEmpty(dbItem[uiField.ID]) && dbItem[uiField.ID] == uiField.Value)
{
continue;
}
else
{
if (title == “FieldName1”)
{
uiField.Value = dbItem.Fields[“FieldName1”].Value;
}
if (title == “FieldName2”)
{
uiField.Value = dbItem.Fields[“FieldName2”].Value;
}
}

}
}
}
}

}
}

That’s it. Let me know if you have any questions.

cache

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s