On save event, I want to get item's property value before it's modified. Concretely, when a folder is saved, I want to judge if the folder' metadata schema is modified or not by comparing metadata schema's title "before" saved and "after" saved. Following source code seems working, so I think there are following facts.
- On "Initiated" phase, value "before" modified is retrieved.
- On "Processed" phase, value "after" modified is retrieved.
Is this understanding correct?
Does my source code really work well?
private void Subscribe()
{
EventSystem.Subscribe<Folder, SaveEventArgs>(OnInitiatedSave, EventPhases.Initiated);
EventSystem.Subscribe<Folder, SaveEventArgs>(OnProcessedSave, EventPhases.Processed);
}
private void OnInitiatedSave(Folder folder, SaveEventArgs args, EventPhases phase)
{
if (args.IsNewItem)
{
return;
}
Folder preFolder = new Folder(folder.Id, folder.Session);
if (preFolder.MetadataSchema == null)
{
return;
}
args.ContextVariables.Add("PreFolderMetadataSchemaTitle", preFolder.MetadataSchema.Title);
}
private void OnProcessedSave(Folder folder, SaveEventArgs args, EventPhases phase)
{
string preFolderMetadataSchemaTitle = null;
if (args.ContextVariables.ContainsKey("PreFolderMetadataSchemaTitle"))
{
preFolderMetadataSchemaTitle = (string)args.ContextVariables["PreFolderMetadataSchemaTitle"];
}
if (folder.MetadataSchema != null)
{
// Compare folder title to previous folder title.
if (folder.MetadataSchema.Title.Equals(preFolderMetadataSchemaTitle)))
{
// process
}
}
}