How does Sitecore Update Package Work – Creating Field Commands

Posted 10/14/2016 by Subramanian Ramanathan

We have created Item Commands & Version Commands in previous blogs and in continuation to it, we can create Field 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 Field Commands AddFieldCommand, DeleteFieldCommand and ChangeFieldCommand.

Before creating Field Commands, we will have to create SyncField,

SyncField syncField = SyncField.BuildField("{C30A013F-3CC8-4961-9837-1C483277084A}", "Title", "title", "New Title", true);

Adding or Deleting a Field Value will be simple,

AddFieldCommand addFieldCommand = new AddFieldCommand(dataItem, syncField);
DeleteFieldCommand deleteFieldCommand = new DeleteFieldCommand(dataItem, syncField);

For creating ChangeFieldCommand, we have to create a source field and target field.

SyncField sourceField = SyncField.BuildField("{AC3FD4DB-8266-476D-9635-67814D91E901}", "Summary", "summary", "Old Summary", true);
SyncField targetField = SyncField.BuildField("{AC3FD4DB-8266-476D-9635-67814D91E901}", "Summary", "summary", "New Summary", true);
ChangeFieldCommand changeFieldCommand = new ChangeFieldCommand(dataItem, sourceField, targetField);

If the field doesn’t exist or old value is different, collision will appear accordingly while analyzing the package.

Similar to Item Commands, Collision Behavior for Field 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'.

//Updates with New Value only if Old Value is not different
changeFieldCommand.CollisionBehavior = CollisionBehavior.Skip;

After creating the Field Commands, it has to be added to the ChangeVersionCommand and ChangeItemCommand before creating the update package.

ChangeVersionCommand changeVersionCommand = new ChangeVersionCommand(dataItem, dataItem, SyncVersion.BuildVersion("en-US", "1", string.Empty), SyncVersion.BuildVersion("en-US", "1", string.Empty));

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

We have created the Field Commands, but what if the field is a shared field.

In that case we will not add the created Field Command to ChangeVersionCommand, instead we would be adding them directly into the ChangeItemCommand, since shared field will be common to all the language versions.

//Shared Field Commands are directly added to ChangeItemCommand
List innerCommands = new List();
ChangeItemCommand changeItemCommand = new ChangeItemCommand(dataItem, innerCommands); 


Add your comment