Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

Dev guideRecipesAPI ReferenceChangelog
Dev guideRecipesUser GuidesNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Synchronize Content Events to Optimizely Graph

This Guide shows how to sync Content Management System (CMS 12) Content Events to Graph

Optimizely Graph synchronizes Content Management System (CMS) content events to keep your Graph index up to date. You can rely on automatic synchronization for standard content events or use manual synchronization for advanced scenarios.

Automatic synchronization for Content Events

Optimizely Graph listens for CMS content events and synchronizes content automatically when you perform any of the following actions:

  • Save or publish content
  • Move content
  • Delete content
  • Expire content

This automatic process ensures that your Graph index stays up to date without requiring manual intervention or additional configuration.

Example: Custom logic before synchronization (optional)

You can extend the standard event handling only if you need to add custom business logic before synchronization occurs. For example, to validate or transform content data.

📘

Note

Adding custom handlers is not required for synchronization. The following example is for advanced use cases only.

// These CMS events trigger Graph synchronization:
// - IContent.SavedContent
// - IContent.PublishedContent  
// - IContent.MovedContent
// - IContent.DeletedContent
// - IContent.ExpiredContent

// Example: Manual event handling
public class CustomContentEventHandler : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        var events = context.Locate.Advanced.GetInstance<IContentEvents>();
        
        events.PublishedContent += (sender, args) =>
        {
            // Custom logic before Graph sync
            if (args.Content is ArticlePage article)
            {
                // Validate or transform content
                Logger.Information($"Publishing article: {article.Heading}");
            }
        };
    }
}

Manual sync for Content Events

If you need to reindex content manually, for example, after a bulk import or when running a custom job, you can use the IContentIndexer service to trigger synchronization programmatically.

📘

Note

Manual synchronization is optional. Regular content changes do not require this.

// Inject the Content Graph service
public class ContentSyncService
{
    private readonly IContentIndexer _contentIndexer;
    private readonly IContentRepository _contentRepository;
    
    public ContentSyncService(
        IContentIndexer contentIndexer,
        IContentRepository contentRepository)
    {
        _contentIndexer = contentIndexer;
        _contentRepository = contentRepository;
    }
    
    // Sync specific content item
    public async Task SyncContentAsync(ContentReference contentRef)
    {
        var content = _contentRepository.Get<IContent>(contentRef);
        await _contentIndexer.IndexAsync(content.ContentLink);
    }
    
    // Bulk sync multiple content items
    public async Task SyncContentAsync(IEnumerable<ContentReference> contentRefs)
    {
        var contentItems = contentRefs
            .Select(contentRef => _contentRepository.Get<IContent>(contentRef))
            .Where(content => content is PageData || content is BlockData);
            
        await _contentIndexer.IndexAsync(contentItems.Select(m => m.ContentLink));
    }
}