Read Package

Reading Update Package - Implementing Field Commands

Posted 12/19/2016 by Subramanian Ramanathan

This blog describes reading Field Commands and Property Commands from an Update Package. 

First we retrieve the version of the item to be updated using the ID, Language and Version from Command.

Item itemVersionToChange = master.GetItem(new Sitecore.Data.ID(fieldCommand.ItemID), Sitecore.Globalization.Language.Parse(fieldCommand.Language), Sitecore.Data.Version.Parse(fieldCommand.Version));

We have three field commands, AddFieldCommand, DeleteFieldCommand and ChangeFieldCommand. The Field Commands exist inside ChangeVersionCommand and Shared Field Commands exist inside ChangeItemCommand.Field commands can be determined using the CommandPrefix property.

Field Command

AddFieldCommand:

Value to be added to the field can be retrieved from the AddFieldCommand. If the target field already has a value and if the collision behavior is set to Skip for the AddFieldCommand, field update can be skipped.

if (change.CommandPrefix == (new AddFieldCommand()).CommandPrefix)
{
    AddFieldCommand addFieldCommand = (AddFieldCommand)change;
    Sitecore.Data.Fields.Field fieldToBeAdded = itemVersionToChange.Fields[new Sitecore.Data.ID(addFieldCommand.AddedField.FieldID)];
    if (!(change.CollisionBehavior == CollisionBehavior.Skip && fieldToBeAdded != null && fieldToBeAdded.HasValue))
    {
        //Adding Field Value to Item
        fieldToBeAdded.Value = addFieldCommand.AddedField.FieldValue;
    }
}

DeleteFieldCommand:

Field whose value has to be cleared, can be retrieved from the DeleteFieldCommand,

if (change.CommandPrefix == (new DeleteFieldCommand()).CommandPrefix)
{
    //Clearing Field Value in Item
    DeleteFieldCommand deleteFieldCommand = (DeleteFieldCommand)change;
    itemVersionToChange.Fields[new Sitecore.Data.ID(deleteFieldCommand.FieldID)].Value = string.Empty;
}

ChangeFieldCommand:

Value to be changed can be retrieved from the ChangeFieldCommand. If the field’s old value is different and if the collision behavior is set to Skip for the ChangeFieldCommand, field update can be skipped.

if (change.CommandPrefix == (new ChangeFieldCommand()).CommandPrefix)
{
    //Changing Field Value from Old Value to New Value
    ChangeFieldCommand changeFieldCommand = (ChangeFieldCommand)change;
    Sitecore.Data.Fields.Field fieldToBeChanged = itemVersionToChange.Fields[new Sitecore.Data.ID(changeFieldCommand.FieldID)];
    foreach (ChangeEntry entry in changeFieldCommand.Changes)
    {
        if (!(change.CollisionBehavior == CollisionBehavior.Skip && fieldToBeChanged != null && fieldToBeChanged.Value != entry.OldValue))
        {
            fieldToBeChanged.Value = entry.NewValue;
        }
    }
}

ChangePropertyCommand:

This command has the properties of Item like Item Name, Item Path, Template etc. This command only has the Item ID, since the properties are common to all the languages and versions of Item. If old value of the property is different and if the collision behavior is set to Skip, then update to the property can be skipped.

if(command.CommandPrefix == (new ChangePropertyCommand()).CommandPrefix)
{
    ChangePropertyCommand changePropertyCommand = (ChangePropertyCommand)command;
    Item itemVersionToChange = master.GetItem(new Sitecore.Data.ID(changePropertyCommand.ItemID));
    foreach (ChangeEntry entry in changePropertyCommand.Changes)
    {
        //Changing Properties like Item Name, Item Path, Template ID etc..
        if (!(changePropertyCommand.CollisionBehavior == CollisionBehavior.Skip && entry.Name.ToLowerInvariant() == "name" && itemVersionToChange.Name != entry.OldValue))
        {
            itemVersionToChange.Name = entry.NewValue;
        }
        else if (!(changePropertyCommand.CollisionBehavior == CollisionBehavior.Skip && entry.Name.ToLowerInvariant() == "templateid" && itemVersionToChange.TemplateID.ToString() != entry.OldValue))
        {
            itemVersionToChange.TemplateID = new Sitecore.Data.TemplateID(new Sitecore.Data.ID(entry.NewValue));
        }
    }
}

Share:

Add your comment

 
 

 

Archive

Syndication