Take the community feedback survey now.

sunylcumar
May 18, 2025
  445
(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
Optimizely CMS Mixed Auth - Okta + ASP.NET Identity

Configuring mixed authentication and authorization in Optimizely CMS using Okta and ASP.NET Identity.

Damian Smutek | Oct 27, 2025 |

Optimizely: Multi-Step Form Creation Through Submission

I have been exploring Optimizely Forms recently and created a multi-step Customer Support Request Form with File Upload Functionality.  Let’s get...

Madhu | Oct 25, 2025 |

How to Add Multiple Authentication Providers to an Optimizely CMS 12 Site (Entra ID, Google, Facebook, and Local Identity)

Modern websites often need to let users sign in with their corporate account (Entra ID), their social identity (Google, Facebook), or a simple...

Francisco Quintanilla | Oct 22, 2025 |

Connecting the Dots Between Research and Specification to Implementation using NotebookLM

Overview As part of my day to day role as a solution architect I overlap with many clients, partners, solutions and technologies. I am often...

Scott Reed | Oct 22, 2025

MimeKit Vulnerability and EPiServer.CMS.Core Dependency Update

Hi everyone, We want to inform you about a critical security vulnerability affecting older versions of the EPiServer.CMS.Core  package due to its...

Bien Nguyen | Oct 21, 2025

Speeding Up Local Development with a Fake OpenID Authentication Handler

When working with OpenID authentication, local development often grinds to a halt waiting for identity servers, clients, and users to be configured...

Eric Herlitz | Oct 20, 2025 |