Working With Custom Hooks: OnRollback
Performing a rollback on a content item in the Site Tree reverts the content item to a previous version. Learn how to enhance this custom hook to meet business requirements and avoid content management mishaps..
Performing a rollback on a content item in the Site Tree reverts the content item to a previous version. A user might perform a rollback in the CMS because they want to undo the changes that were made in a checked-out version of a content item in the Site Tree.
When a user does a rollback on a content item it has to be checked out and assigned to them. When a rollback is performed on a content item, the current checked out version of the item is replaced with the content of the most recently checked in version’s content. This action cannot be undone and the content of the checked out version that is replaced is deleted irretrievably. Rollback leaves the item checked out to the user at the end of the process.
For a video demonstration of how this works, see [enter video link here]
Users can trigger this action manually using the following methods:
- Right-clicking on a content item in the Site Tree and selecting rollback from the context menu.
- Having a script use the rollback method in the CSAPI.
Considerations
Rollback is not often used in the CMS and is only accessible through a single interface, so this hook won’t be manually triggered often.
Always avoid calling the methods that perform a rollback in the CSAPI from within these rollback hooks to avoid infinite loops.
It’s important to note that rollback and revert are not the same thing. Performing a revert from the History Tab or other means does not trigger the rollback hook.
Examples
Prevent Rollback to Previous Schema Versions
Just like content items in the Site Tree, schemas have versions. When a developer makes a change to a schema (such as adding or removing content fields) the version of the schema changes by auto-incrementing.
Content items are created to match the current version of the schema and can be synced to match newer versions of schemas by a developer.
Some actions, including rollback, can take a content item back to a version that isn’t on the current schema. This script prevents the rollback from taking place if the current version of the schema doesn’t match the earlier version of the schema that the rollback action would have reverted to.
int currentVersion = page.LastVersion.SchemaVersion.VersionNumber;
int pagesLastVersion = page.CheckedInVersion().SchemaVersion.VersionNumber;
if (pagesLastVersion != currentVersion)
{
throw new Exception("Cant Rollback the current version isn’t on the same schema version as the current schema version." );
}
The code above gets the two values we need to compare and then throws an error if the versions don’t match with a message that the versions don’t match. Any error thrown will cancel the execution of the action that triggered the script.
This script could be enhanced in the following ways:
- Instead of canceling, write a log that the item was rolled back and is out of sync with the schema so it could be put into sync by a developer or an administrator.
- Only allow rollback by users that are developers or administrators and would understand and have to the ability to put the item back in sync.
There are no comments yet.