How does Sitecore Update Package Work – Creating Item Commands

Posted 10/05/2016 by Subramanian Ramanathan

As we discussed in the previous blog, Update Package will not have items, but will have only the commands like AddItemCommand, DeleteFieldCommand etc. This blog explains how to create Item Commands like AddItemCommand, DeleteItemCommand and ChangeItemCommand. We will have to create ContentDataItem for creating Item Commands

ContentDataItem expects either the SyncItem from Sitecore or the Serialized File of Item. If we have the item in Sitecore, we can build the SyncItem and create the ContentDataItem.

SyncItem syncItem = ItemSynchronization.BuildSyncItem(Utilities.GetItem(new Sitecore.Data.ID("{…}")));
ContentDataItem dataItem = new ContentDataItem(string.Empty, string.Empty, string.Empty, syncItem);

If we have to create ContentDataItem using the serialized file of the Item, then we need to pass the rootpath(website folder path), relatedpath(path of the file after rootpath) and filename,

ContentDataItem dataItem = new ContentDataItem(rootPath, relatedPath, name);

Creating Add and Delete Item Commands are simple,

//This will add the Item into target database if it doesn’t exist
AddItemCommand addItemCommand = new AddItemCommand(dataItem);

If the Item already exists, it will be overwritten. This will appear as collision when we analyze this package in the UpdateInstallationWizard.

//This will delete the Item from target database if exists
DeleteItemCommand deleteItemCommand = new DeleteItemCommand(dataItem);

If Item doesn’t exist, a collision will appear while analyzing the package

For creating a ChangeItemCommand, we have to create the SyncItem using the same Item and apply the required changes (that have to be applied in the target database item) in the newly created SyncItem. ChangeItemCommand will automatically generate the appropriate Commands(like ChangeFieldCommand, ChangeVersionCommand, DeleteVersionCommand etc.) based on the SyncItem Changes specified.

//Target SyncItem is created. Field Value & Language is modified
SyncItem targetItem = ItemSynchronization.BuildSyncItem(Utilities.GetItem(new Sitecore.Data.ID("{…}")));
targetItem.Versions.Last().Fields.SingleOrDefault(i => i.FieldID == "{…}").FieldValue = "New Value";
targetItem.Versions.Last().Language = "en-IN";
ContentDataItem targetDataItem = new ContentDataItem(string.Empty, string.Empty, string.Empty, targetItem);

//This will change the field value and language
ChangeItemCommand changeItemCommand = new ChangeItemCommand(dataItem, targetDataItem);

This will generate appropriate Field Commands and Version Commands inside ChangeItemCommand automatically. If the item doesn’t exist or old value is different, collision will appear while analyzing the package

Collision Behavior for Item Commands can be Force(default) or Skip.
If Collision Behavior is not set or is set to Force, then the existing items will be overwritten if there is a collision and the Action would be 'Overwrite Existing'. If Collision Behaviour is set to Skip, then the existing items will not be updated if there is a collision and the Action would be 'Keep Existing'.

//Adds the Item only if Item doesn't exist
addItemCommand.CollisionBehavior = CollisionBehavior.Skip;


We can discuss more on manually creating Version Commands, Field Commands & Creating Update Package in the upcoming blogs.


Add your comment