Reading Update Package – Implementing Item Commands

As we know we have three item commands, AddItemCommand, DeleteItemCommand and ChangeItemCommand. Item commands can be determined using the CommandPrefix property.

AddItemCommand:

Item to be added can be retrieved from the AddItemCommand. If the Item already exists and if the collision behavior is set to Skip for the AddItemCommand, item addition can be skipped. Item can be overwritten if collision behavior is set to force. Versions and Field values will be retrieved from Command using AddedItem property and added to the target database using PasteSyncItem method of ItemSynchronization class.

if (command.CommandPrefix == (new AddItemCommand()).CommandPrefix)
{
    SyncItem syncItemToBeAdded = ((AddItemCommand)command).AddedItem;
    LoadOptions options = new LoadOptions();
    options.Database = master;
    options.ForceUpdate = true;
    options.UseNewID = false;
 
    //Adding Item in the Target Database
    if (!(command.CollisionBehavior == CollisionBehavior.Skip && master.Items.Exists(new Sitecore.Data.ID(syncItemToBeAdded.ID))))
        ItemSynchronization.PasteSyncItem(syncItemToBeAdded, options);
}

DeleteItemCommand:

ID of the Item to be deleted can be retrieved from the DeleteFieldCommand,

if (command.CommandPrefix == (new DeleteItemCommand()).CommandPrefix)
{
    //Deleting Item from Target Database
    DeleteItemCommand deleteItemCommand = (DeleteItemCommand)command;
    if (master.Items.Exists(new Sitecore.Data.ID(deleteItemCommand.ItemID)))
        master.GetItem(new Sitecore.Data.ID(deleteItemCommand.ItemID)).Delete();
}

ChangeItemCommand:

Item to be changed can be retrieved from ChangeItemCommand. Version, Shared Fields and Properties to be changed has to be retrieved from the Commands property of ChangeItemCommand.

if (command.CommandPrefix == (new ChangeItemCommand()).CommandPrefix)
{
    ChangeItemCommand changeItemCommand = (ChangeItemCommand)command;
    if (master.Items.Exists(new Sitecore.Data.ID(changeItemCommand.ItemID)))
    {
        //Retrieving Inner Commands of ChangeItemCommand
        List<icommand> innerCommands = changeItemCommand.Commands.ToList();
        foreach(ICommand innerCommand in innerCommands)
        {
            //Executing FieldCommand of Shared Field
            ExecuteFieldCommand(innerCommand);
            ExecuteVersionCommand(innerCommand);
 
        }
    }
}

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

Originally Posted at:  https://www.nttdatasitecore.com/Blog/2016/December/Reading-Update-Package-Implementing-Item-Commands

Leave a Reply

Your email address will not be published. Required fields are marked *