Take the community feedback survey now.

sunylcumar
May 18, 2025
  392
(3 votes)

Indexing a content item programatically

public bool IndexContent(int contentId, bool contentOnly, bool childrenOnly, string language)
{
    // Retrieve the content
    var contentReference = new ContentReference(contentId);
    var contentItems = new List<IContent>();

    if (contentReference != null && contentReference != ContentReference.EmptyReference)
    {
        if (contentOnly)
        {
            var contentItem = _contentRepository.Get<IContent>(contentReference);
            try
            {
                // Index the content
                _contentIndexer.Index(contentItem);
                return true;
            }
            catch (Exception ex)
            {
                _logger.Error($"Error in IndexContent, exception is {ex}");
                return false;
            }
        }
        else
        {
            if (childrenOnly)
            {
                //Get the children of the content
                var children = _contentRepository.GetChildren<IContent>(contentReference);
                if (children != null && children.Any())
                    contentItems = children.ToList();
            }
            else
            {
                //Get the descendants of the content
                var descendants = _contentRepository.GetDescendents(contentReference).Select(_contentRepository.Get<IContent>);
                if (descendants != null && descendants.Any())
                    contentItems = descendants.ToList();
            }
            // Save the updated content
            if (contentItems != null && contentItems.Count > 0)
            {
                foreach (var contentItem in contentItems)
                {
                    try
                    {
                        // Index the content
                        _contentIndexer.Index(contentItem);
                    }
                    catch (Exception ex)
                    {
                        _logger.Error($"Error in IndexContent, exception is {ex}");
                    }
                }
                return true;
            }
        }
    }
    return false;
}
May 18, 2025

Comments

El Magnifico
El Magnifico May 18, 2025 02:27 PM

good example

Please login to comment.
Latest blogs
Meet the newest OMVPs – summer 2025 cohort

We’re excited to welcome the latest group of Optimizely Most Valuable Professionals (OMVPs) into the program! This summer’s cohort highlights a ble...

Satata Satez | Sep 5, 2025

The Sweet Spot: Hybrid Headless Architecture

When it comes to content management architecture, the pendulum often swings between tightly coupled “headed” CMS setups and the flexibility of full...

Minesh Shah (Netcel) | Sep 4, 2025

Preview Unpublished Pages and Blocks on the Frontend (Optimizely CMS 12)

Introduction In my previous post , I explained how to customize the ContentArea rendering pipeline in Optimizely CMS 12 so editors can see...

Adnan Zameer | Sep 4, 2025 |

How to automatically remove orphaned Opti jobs from the DB

Optimizely CMS provides a simple yet powerful built-in job system that handles most standard scheduling scenarios with ease. Developers can easily...

Stanisław Szołkowski | Sep 4, 2025 |