sunylcumar
May 18, 2025
visibility 884
star star star star star
(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

error Please login to comment.
Latest blogs
Is your website agent-ready? Using Cloudflare's readiness tool to assess your website

Part one: the visitor you didn't design for For twenty-five years we have built websites for two audiences: people, and the search engines that sen...

Scott Reed | Oct 29, 2026

Visual Builder without Next.js, Part 1: What Astro Has to Rebuild on CMS 13 PaaS

Series: Part 1: What Astro Has to Rebuild on CMS 13 PaaS · Part 2: Does It Survive DXP? (Redis-backed ISR, Graph webhooks, Cloudflare and preview a...

Piotr | Oct 29, 2026

Optimizely certifications in 2026: the new programme explained

How Optimizely's 2026 certification programme works: Foundation, Core and Advanced tiers, 17 product tracks, renewal, and which path to take.

Matthew Dunn | Oct 29, 2026 |

Visual Builder without Next.js, Part 1: What Astro Has to Rebuild on CMS 13 PaaS

A field report on the framework-neutral CMS SDK 3.1 beta: Visual Builder, the content model and live preview in Astro, on a real CMS 13.

Piotr | Oct 29, 2026 |