Data Imports in Optimizely: Part 2 - Store a persistent identifier

When importing content, frequently it will be necessary to run the import multiple times, updating the data each time. The only other alternative is to delete all the content and start fresh with new items, which is a costly way to implement it.

The solution to this is to store a persistent identifier that correlates data in your source system with the corresponding page in Optimizely.

First you’ll create the identifier on the Optimizely page:

[ContentType(
    DisplayName = "Imported Page",
    GUID = "fcc5028e-a461-47fb-8f8e-bf12fcbbe925"
)]
public class ImportedPage : PageData
{
    [Display(
        Name = "Legacy ID",
        GroupName = SystemTabNames.Content,
        Order = 10
    )]
    [Editable(false)]
    public virtual int LegacyID { get; set; }
}

Then, when importing, you’ll need to lookup the item and conditionally create it if it does not exist:

var id = importData.Id;

// Implement method to fetch the page by ID
var page = GetPageById<ImportedPage>(id);

if (page == null) {
    page = _contentRepository.GetDefault<ImportedPage>(importFolder);
}

page = (ImportedPage)page.CreateWriteableClone();

if (page.LegacyID != id) {
    page.LegacyID = id;
}

// Update fields

_contentRepository.Save(page, SaveAction.Publish | SaveAction.Patch, AccessLevel.NoAccess);

A few points to remember:

  1. It is recommended to make the field non-editable in the UI to avoid accidentally updating the ID and mixing up content.

  2. Don’t use [CultureSpecific] - the ID should not be dependent on the language.

  3. Choose the appropriate data type - some systems such as Sitecore may use GUIDs in which case a string field would be more appropriate, whereas others may use integers.

Previous
Previous

Data Imports in Optimizely: Part 3 - Query data efficiently

Next
Next

Data Imports in Optimizely: Part 1 - Writing efficient data imports