Tuesday, December 10, 2013

Tridion 2013 SP1 - Synchronizing components

Continuing with my posts about Tridion 2013 SP1, I will talk about the new Synchronization API. This API that is available via the Core Services making this API easy to use and very light.

The new synchronization functionality will analyze the component content and the schema and it will infer which operations need to be completed in order to keep them in sync.

Sample:

private static CoreServiceClient channel { get; set; }

using (channel = new CoreServiceClient("basicHttp_2013"))
    try {
        SynchronizeComponent("tcm:5-2051", true);
    }
    finally {
        if (channel.State != CommunicationState.Closed) {
            channel.Close();
        }
    }
}

public static void SynchronizeComponent(string componentId, bool updateAfterSynchronize) {
    Console.WriteLine("Starting Sychronization...");

    SynchronizeOptions options = new SynchronizeOptions() {
        SynchronizeFlags = SynchronizeFlags.All
    };

    SynchronizationResult result;
    if (updateAfterSynchronize) {
        result = channel.SynchronizeWithSchemaAndUpdate(componentId, options);
    }
    else {
        IdentifiableObjectData item = channel.Read(componentId, new ReadOptions());
        result = channel.SynchronizeWithSchema(item, options);
    }

    foreach(SynchronizationActionData action in result.SynchronizationActions) {
        Console.WriteLine();

        Console.WriteLine("Field Name: {0}", action.FieldName);
        Console.WriteLine("Field Index: {0}", action.FieldIndex);
        Console.WriteLine("Action Taken: {0}", action.SynchronizationActionApplied);

        Console.WriteLine();
    }

    Console.WriteLine("Sychronization completed.");
    Console.ReadLine();
}

The first thing we need to consider while writing a synchronizing program would be the Synchronization Options, basically the Synchronization flags.

SynchronizeFlags.All
SynchronizeFlags.ApplyDefaultValuesForMissingMandatoryFields
SynchronizeFlags.ApplyDefaultValuesForMissingNonMandatoryFields
SynchronizeFlags.ApplyFilterXsltToXhtmlFields
SynchronizeFlags.Basic
SynchronizeFlags.ConvertFieldType
SynchronizeFlags.FixNamespace
SynchronizeFlags.RemoveAdditionalValues
SynchronizeFlags.RemoveUnknownFields
SynchronizeFlags.UnknownByClient

You must be careful while using those flags since some of them like  RemoveUnknownFields that will lead you to lose data if is not used carefully.

Other important feature in this new API is the fact that this will return feedback in the form of SynchronizationActionData objects. As you can see in the sample above it will give you information like the FileName, FileIndex and the action that was taken.

You may also notice that in the sample above I am using two different methods SycnrhonizeWithSchema and SynchronizeWithSchemaAndUpdate, the difference is clear, the first one won’t check in any changes and the second one will check in the changes leading you to lose data if not used carefully.

2 comments:

  1. Much appreciated 2013 SP1 posts, Eric. Thanks!

    When you mentioned this part earlier, we updated the PowerTools page. http://code.google.com/p/tridion-2011-power-tools/

    "(06-Dec 2013) SDL Tridion 2013 adds Content Schema Synchronization methods to CoreService? and TOM.NET. This is not a component synchronizer on its own, but we'll have an easier way to move forward with PowerTools Component Synchronizer (community willing)."

    ReplyDelete
  2. Great post Eric, really helped me a lot.

    ReplyDelete