How does Sitecore Update Package Work – Creating Version Commands

Posted 10/09/2016 by Subramanian Ramanathan

We have created Item Commands in our previous blog and in continuation to it, we would be creating Version Commands in this blog.
Let’s create ContentDataItem,

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

Similar to Item Commands, we have three Version Commands AddVersionCommand, DeleteVersionCommand and ChangeVersionCommand. Before creating Version Commands, we will have to create SyncVersion,

SyncVersion syncVersion = SyncVersion.BuildVersion("en", "1", string.Empty);
//Adding Fields to the SyncVersion
syncVersion.AddField("{C30A013F-3CC8-4961-9837-1C483277084A}", "Title", "title", "New Title", true);
syncVersion.AddField("{AC3FD4DB-8266-476D-9635-67814D91E901}", "Summary", "summary", "New Summary", true);

Adding or Deleting a Version will be simple,

AddVersionCommand addVersionCommand = new AddVersionCommand(dataItem, syncVersion);
DeleteVersionCommand deleteVersionCommand = new DeleteVersionCommand(dataItem, syncVersion);

For creating ChangeVersionCommand, we have to create a source version and target version.

SyncVersion sourceVersion = syncItem.Versions.Single(i => i.Language == "es-MX" && i.Version == "1");
//Copying the Source Version to Target Version
SyncVersion targetVersion = SyncVersion.BuildVersion("es-MX", "1", string.Empty);
foreach(SyncField field in sourceVersion.Fields)
    targetVersion.AddField(field.FieldID, field.FieldName, field.FieldKey, field.FieldValue, true);

//Adding a field value
targetVersion.AddField("{...}", "fieldname", "fieldkey", "fieldvalue", true);
//Removing a field value
//Updating a field value
targetVersion.Fields.Single(i => i.FieldID == "{..}").FieldValue = "Updated Value";

ChangeVersionCommand changeVersionCommand = new ChangeVersionCommand(dataItem, null, sourceVersion, targetVersion);

This will generate AddFieldCommand/DeleteFieldCommand/ChangeFieldCommand inside ChangeVersionCommand. If the item doesn’t exist or old value is different, collision will appear accordingly while analyzing the package.

Similar to Item Commands, Collision Behavior for Version 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 Version only if Version doesn't exist
addVersionCommand.CollisionBehavior = CollisionBehavior.Skip;


After creating the Version Commands, it has to be added to the ChangeItemCommand before creating the update package.

List innerCommands = new List();
ChangeItemCommand changeItemCommand = new ChangeItemCommand(dataItem, innerCommands);

We can discuss on Creating Field Commands and Shared Field Commands in the upcoming blogs.


Add your comment