Read Package

Reading Update Package - Implementing Version Commands

Posted 12/19/2016 by Subramanian Ramanathan

As we know we have three version commands, AddVersionCommand, DeleteVersionCommand and ChangeVersionCommand. Version Commands will exist inside ChangeItemCommand. Version commands can be determined using the CommandPrefix property.

Version Command

AddVersionCommand:

Version to be added and the target Item can be retrieved from the AddVersionCommand using ID, Language and Version. If the target Version already exists and if the collision behavior is set to Skip for the AddVersionCommand, version addition can be skipped. Version can be overwritten if collision behavior is set to force. Field values will be retrieved from Command using AddedVersion.Fields property and added to the newly created version.

if (command.CommandPrefix == (new AddVersionCommand()).CommandPrefix)
{
    AddVersionCommand addVersionCommand = (AddVersionCommand)command;
    Item itemVersionToAdd = master.GetItem(new Sitecore.Data.ID(addVersionCommand.ItemID), Sitecore.Globalization.Language.Parse(addVersionCommand.Language), Sitecore.Data.Version.Parse(addVersionCommand.Version));
    if (!(command.CollisionBehavior == CollisionBehavior.Skip && itemVersionToAdd != null))
    {
        //Adding Versions & Field Values to Item
        foreach (SyncField syncFieldToBeAdded in addVersionCommand.AddedVersion.Fields)
        {
            itemVersionToAdd.Fields[new Sitecore.Data.ID(syncFieldToBeAdded.FieldID)].Value = syncFieldToBeAdded.FieldValue;
        }
    }
}

DeleteVersionCommand:

Version to be deleted can be retrieved from the DeleteFieldCommand using ID, Language and Version,

if (command.CommandPrefix == (new DeleteVersionCommand()).CommandPrefix)
{
    DeleteVersionCommand deleteVersionCommand = (DeleteVersionCommand)command;
    Item itemVersionToDelete = master.GetItem(new Sitecore.Data.ID(deleteVersionCommand.ItemID), Sitecore.Globalization.Language.Parse(deleteVersionCommand.Language), Sitecore.Data.Version.Parse(deleteVersionCommand.Version));
    if (itemVersionToDelete != null)
    {
        //Removing Version from Item
        itemVersionToDelete.Versions.RemoveVersion();
    }
}

ChangeVersionCommand:

Version to be changed can be retrieved from ChangeVersionCommand. Fields of the Version to be changed can be retrieved from the Changes property of ChangeVersionCommand.

if (command.CommandPrefix == (new ChangeVersionCommand()).CommandPrefix)
{
    ChangeVersionCommand changeVersionCommand = (ChangeVersionCommand)command;
    Item itemVersionToChange = master.GetItem(new Sitecore.Data.ID(changeVersionCommand.ItemID), Sitecore.Globalization.Language.Parse(changeVersionCommand.Language), Sitecore.Data.Version.Parse(changeVersionCommand.Version));
    itemVersionToChange.Editing.BeginEdit();
    foreach (ICommand change in changeVersionCommand.Changes)
    {
        //Executing FieldCommands of Item Fields
        ExecuteFieldCommands(change);
    }
    itemVersionToChange.Editing.EndEdit();
}

We would be discussing about Field Commands and Property Commands in detail in the upcoming blogs.

Share:

Add your comment

 
 

 

Archive

Syndication