2

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
        }
    }
}

1 Answer 1

2

Your assumption about the Event Phases looks correct to me. The only thing I want to point out is, EventPhases.Processed represents the phase where the event is processed but the changes are not committed to the Content Manager. So if you want to execute your handler once the changes are committed to Content Manager, use EventPhases.TransactionCommitted.

As far your code is concerned it looks good and should run. I haven't run it on my machine though.

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.