sunylcumar
May 18, 2025
  264
(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
Create a multi-site aware custom search provider using Search & Navigation

In a multisite setup using Optimizely CMS, searching for pages can be confusing. The default CMS search regardless of search provider does not...

dada | Jun 12, 2025

Tunning Application Insights telemetry filtering in Optimizely

Application Insights is a cloud-based service designed to monitor web applications, providing insights into performance, errors, and user behavior,...

Stanisław Szołkowski | Jun 12, 2025 |

JavaScript SDK v6: Lightest, Most Efficient SDK Yet

Need a faster site and less frontend bloat? JavaScript SDK v6 is here —and it’s the lightest, smartest SDK we’ve ever released for Optimizely Featu...

Sarah Ager | Jun 11, 2025

Boosting Indexing Efficiency: Reindex Pages Directly from Optimizely’s Navigation Pane

There can be various reasons why you might want to trigger indexing or reindexing of a page/node directly from the navigation pane. In my case, we...

Praful Jangid | Jun 11, 2025