Deploying objects with custom ID fields
When transferring data between instances of Xperience (via import/export or staging), the IDs of objects can differ between the environments. Xperience automatically “translates” the ID values for system objects, but not for custom objects. For example, when you stage data of a custom table or page type that contains a custom field storing object IDs, the values may not match the IDs of the objects on the target server.
You can ensure that the system correctly translates custom ID fields by handling the events in the ColumnsTranslationEvents class:
- RegisterRecords – occurs on the source application when exporting objects. Handle this event to provide information for custom field translation.
- TranslateColumns – occurs on the target application when importing objects. Handle this event to translate field values using the provided information.
Important
Handle the RegisterRecords event on the source application, and the TranslateColumns event on the target application. Add the handlers to both applications to ensure correct ID translation for both directions (e.g., in bi-directional staging environments).
Example
The following example demonstrates how to implement custom ID translation for the CustomTableUserID field of a custom table. The field stores the IDs of the users who own individual records in the custom table.
Open your Xperience administration solution in Visual Studio (using the WebApp.sln file).
Create a custom module class.
- Add the class into a custom Class Library project within the solution.
Override the module’s OnInit method and assign a handlers to the ColumnsTranslationEvents.RegisterRecords.Execute and ColumnsTranslationEvents.TranslateColumns.Execute events.
using CMS; using CMS.DataEngine; using CMS.CustomTables; using CMS.Helpers; using CMS.Membership; // Registers the custom module into the system [assembly: RegisterModule(typeof(CustomIDTranslationModule))] public class CustomIDTranslationModule : Module { // Module class constructor, the system registers the module under the name "CustomIDTranslation" public CustomIDTranslationModule() : base("CustomIDTranslation") { } // Contains initialization code that is executed when the application starts protected override void OnInit() { base.OnInit(); // Add this code on the SOURCE application from which you are exporting the objects // Assigns a handler method to the RegisterRecords event ColumnsTranslationEvents.RegisterRecords.Execute += RegisterRecords_Execute; // Add this code on the TARGET application where you are importing the objects // Assigns a handler method to the TranslateColumns event ColumnsTranslationEvents.TranslateColumns.Execute += TranslateColumns_Execute; } }
Define the handler methods (inside the custom module class):
// Provides the ID translation data when exporting objects private void RegisterRecords_Execute(object sender, ColumnsTranslationEventArgs e) { // Registers custom column translation data for "customtable.sampletable" if (e.ObjectType == CustomTableItemProvider.GetObjectType("customtable.sampletable")) { // Gets the user ID from the custom table's data int userId = ValidationHelper.GetInteger(e.Data.GetValue("CustomTableUserID"), 0); // Prepares parameters for the translation TranslationParameters parameters = new TranslationParameters(UserInfo.OBJECT_TYPE) { CodeName = UserInfoProvider.GetUserNameById(userId) }; // Maps the user ID value to the corresponding username for every record in the custom table's data // Adds the mapping information into the export package e.TranslationHelper.RegisterRecord(userId, parameters); } } // Handles ID translation when importing objects private void TranslateColumns_Execute(object sender, ColumnsTranslationEventArgs e) { // Performs column translation for "customtable.sampletable" if (e.ObjectType == CustomTableItemProvider.GetObjectType("customtable.sampletable")) { // Translates the values of the UserID field in the table's data // Uses the mapping information in the import package to find the correct IDs based on the usernames e.TranslationHelper.TranslateColumn(e.Data, "CustomTableUserID", UserInfo.OBJECT_TYPE, 0, true, true); } }
Save the class and Rebuild the solution.
When you transfer the custom table’s data between the applications using import/export or content staging, the handlers ensure correct translation of ID values for the CustomTableUserID field.