A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

sunylcumar
May 18, 2025
  485
(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
Building simple Opal tools for product search and content creation

Optimizely Opal tools make it easy for AI agents to call your APIs – in this post we’ll build a small ASP.NET host that exposes two of them: one fo...

Pär Wissmark | Dec 13, 2025 |

CMS Audiences - check all usage

Sometimes you want to check if an Audience from your CMS (former Visitor Group) has been used by which page(and which version of that page) Then yo...

Tuan Anh Hoang | Dec 12, 2025

Data Imports in Optimizely: Part 2 - Query data efficiently

One of the more time consuming parts of an import is looking up data to update. Naively, it is possible to use the PageCriteriaQueryService to quer...

Matt FitzGerald-Chamberlain | Dec 11, 2025 |

Beginner's Guide for Optimizely Backend Developers

Developing with Optimizely (formerly Episerver) requires more than just technical know‑how. It’s about respecting the editor’s perspective, ensurin...

MilosR | Dec 10, 2025