Skip to content

Instantly share code, notes, and snippets.

@fluxdigital
Last active September 14, 2022 16:22
Show Gist options
  • Save fluxdigital/dfdefba1d421ffee767d6e8eab6ab702 to your computer and use it in GitHub Desktop.
Save fluxdigital/dfdefba1d421ffee767d6e8eab6ab702 to your computer and use it in GitHub Desktop.
Adds the items to the custom index
public int IndexContent()
{
var indexedCount = 0;
using (new SecurityDisabler())
{
try
{
//get the blog posts from Sitecore
var blogPostsRootPath = _master.GetItem(Settings.GetSetting("BlogPostsRootPath"));
//get all children of the root
var blogPostItems = blogPostsRootPath.Axes.GetDescendants();
foreach (var item in blogPostItems)
{
//check the item is one of the allowed templates
if (blogPostTemplatesToAllow.Contains(item.TemplateID.ToString()))
{
_logService.Info($"Adding Item to Index: {item.Name} - {item.ID} ", this);
//convert to an indexable item
var searchItem = new SitecoreIndexableItem(item);
using (var index = ContentSearchManager.GetIndex(IndexName))
{
var context = index.CreateUpdateContext();
//add the item to the index
index.Operations.Add(searchItem, context, index.Configuration);
context.Commit();
indexedCount +=1;
}
}
}
}
catch (Exception ex)
{
_logService.Error($"error calling IndexContent(): {ex.Message}", ex);
}
}
return indexedCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment