Read Package

Reading Update Package - Implementing Item Commands

Posted 12/19/2016 by Subramanian Ramanathan

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

Item Command

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

Share:

Add your comment

 
 

 

Archive

Syndication