HomeDev GuideRecipesAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Content binding

Learn how content binding in Optimizely CMS connects content items and external data sources to enable automatic synchronization and structured content relationships.

Content binding in Optimizely CMS lets you connect content items to external data sources or other content items. It enables automatic synchronization and structured relationships between content items.

The BindingLoaderOption controls how these bindings apply when content is loaded.

Overview

The BindingLoaderOption class controls whether data bindings apply when loading content. It is part of the Optimizely loader options and integrates with the content binding pipeline.

Key components

  • BindingLoaderOption– Controls binding behavior during content loading.
  • IContentBindable– Interface for content that supports binding.
  • ContentBinding– Represents a binding relationship between content items.
  • BoundedState– Indicates the binding status of content.
  • IContentBinder– Service that performs the binding operations.

Binding loader option

The following code blocks demonstrate how to enable or disable data bindings.

Enable data bindings (default)

var withBindings = BindingLoaderOption.Bounded();
var loaderOptions = new LoaderOptions { withBindings };
var content = contentLoader.Get<IContent>(contentReference, loaderOptions);

Disable data bindings to improve performance

var withoutBindings = BindingLoaderOption.Unbounded();
var loaderOptions = new LoaderOptions { withoutBindings };
var content = contentLoader.Get<IContent>(contentReference, loaderOptions);

Use with other loader options

var loaderOptions = new LoaderOptions 
{ 
    LanguageLoaderOption.Fallback(CultureInfo.GetCultureInfo("en")),
    BindingLoaderOption.Bounded(),
    LoaderOption.ExcludeBlueprints
};

var content = contentLoader.GetChildren<IContent>(parentReference, loaderOptions);

Basic content binding

This section shows fundamental examples of content binding.

Create a product page that binds to a master product

[ContentType]
public class ProductPage : PageData
{
    public virtual string ProductName { get; set; }
    public virtual decimal Price { get; set; }
    public virtual string Description { get; set; }
}

Configure a binding

var productPage = contentRepository.GetDefault<ProductPage>(parentReference);
productPage.Name = "Local Product Instance";

// Bind to master product
var masterProductGuid = Guid.Parse("external content guid");

// Create binding relationship
productPage.Binding = new ContentBinding(
    BindingDefinitionKey: null,
    ContentGuid: masterProductGuid,
    Language: CultureInfo.GetCultureInfo("en")
);

var savedReference = contentRepository.Save(productPage, SaveAction.Publish, AccessLevel.NoAccess);

// Load with binding applied
var boundProduct = contentRepository.Get<ProductPage>(
    savedReference,
    new LoaderOptions { BindingLoaderOption.Bounded() }
);

Advanced content binding examples

This section provides more complex binding scenarios.

Define a source content type

var sourceContentType = new ContentType
{
    Base = ContentTypeBase.Page,
    Name = "SourceArticlePage"
};

sourceContentType.PropertyDefinitions.Add(
    new PropertyDefinition
    {
        Name = "SourceTitle",
        Type = propertyDefinitionTypeRepository.Load(typeof(PropertyLongString))
    }
);

sourceContentType.PropertyDefinitions.Add(
    new PropertyDefinition
    {
        Name = "SourceDescription",
        Type = propertyDefinitionTypeRepository.Load(typeof(PropertyLongString))
    }
);

Define a target content type

var targetContentType = new ContentType
{
    Base = ContentTypeBase.Page,
    Name = "TargetSummaryPage"
};

targetContentType.PropertyDefinitions.Add(
    new PropertyDefinition
    {
        Name = "TargetTitle",
        Type = propertyDefinitionTypeRepository.Load(typeof(PropertyLongString))
    }
);

targetContentType.PropertyDefinitions.Add(
    new PropertyDefinition
    {
        Name = "TargetDescription",
        Type = propertyDefinitionTypeRepository.Load(typeof(PropertyLongString))
    }
);

Create the content binding definition

var bindingDefinition = new ContentBindingDefinition
{
    Key = "ArticleToSummaryBinding",
    SourceContentType = sourceContentType.GUID,
    TargetContentType = targetContentType.GUID,
    PropertyBindingDefinitions = new List<PropertyBindingDefinition>
    {
        new PropertyBindingDefinition
        {
            SourcePropertyDefinition = new PropertyDefinitionReference
            {
                PropertyDefinitionId = sourceContentType.PropertyDefinitions
                    .Single(p => p.Name.Equals("SourceTitle")).ID
            },
            TargetPropertyDefinition = new PropertyDefinitionReference
            {
                PropertyDefinitionId = targetContentType.PropertyDefinitions
                    .Single(p => p.Name.Equals("TargetTitle")).ID
            }
        },
        new PropertyBindingDefinition
        {
            SourcePropertyDefinition = new PropertyDefinitionReference
            {
                PropertyDefinitionId = sourceContentType.PropertyDefinitions
                    .Single(p => p.Name.Equals("SourceDescription")).ID
            },
            TargetPropertyDefinition = new PropertyDefinitionReference
            {
                PropertyDefinitionId = targetContentType.PropertyDefinitions
                    .Single(p => p.Name.Equals("TargetDescription")).ID
            }
        }
    }
};

Save the binding definition

bindingDefinitionRepository.Save(bindingDefinition);

Create source content

var sourceContent = contentRepository.GetDefault<IContent>(
    ContentReference.RootPage,
    sourceContentType.ID
);

sourceContent.Name = "Source Article";
sourceContent.Property["SourceTitle"].Value = "Advanced Content Management Strategies";
sourceContent.Property["SourceDescription"].Value =
    "This guide explains techniques for managing content, including automation, binding, and scalability.";

var sourceContentReference = contentRepository.Save(
    sourceContent,
    SaveAction.Publish,
    AccessLevel.NoAccess
);

Create target content and bind it to the source

var targetContent = contentRepository.GetDefault<IContent>(
    ContentReference.RootPage,
    targetContentType.ID
);

targetContent.Name = "Target Summary";
targetContent.Property["TargetTitle"].Value = "Initial Target Title";
targetContent.Property["TargetDescription"].Value = "Initial target description";

// Create binding relationship
var contentBindable = targetContent as IContentBindable;

contentBindable.Binding = new ContentBinding(
    bindingDefinition.Key,
    sourceContent.ContentGuid,
    sourceContent.Language()
);

var targetContentReference = contentRepository.Save(
    targetContent,
    SaveAction.Publish,
    AccessLevel.NoAccess
);

Execute the binding

var loaderOptions = new LoaderOptions { BindingLoaderOption.Bounded() };
var boundTargetContent = contentRepository.Get<IContent>(targetContentReference, loaderOptions);

// The bound content includes values from the source content.
// Other property values remain unchanged.