Skip to content

Instantly share code, notes, and snippets.

@sniffdk
Last active April 1, 2021 10:32
Show Gist options
  • Save sniffdk/9109832 to your computer and use it in GitHub Desktop.
Save sniffdk/9109832 to your computer and use it in GitHub Desktop.
Small snippet for reindexing an entity in Examine
public void ReIndexEntity(IContentBase entity)
{
if (entity is IContent)
{
ExamineManager.Instance.ReIndexNode((entity as IContent).ToXml(), "content");
}
else if (entity is IMedia)
{
ExamineManager.Instance.ReIndexNode((entity as IMedia).ToXml(), "media");
}
}
// In newer versions of Umbraco 7, the ToXml() method has been declared Obsolete.
// The only thing ToXml() is doing is wrapping the PackagingService --> https://github.com/umbraco/Umbraco-CMS/blob/release-7.2.5/src/Umbraco.Core/Models/ContentExtensions.cs#L816
// So we can just use the Packaging service directly, or wrap it up in our helper method.
public void ReIndexEntityNew(IContentBase entity, IEnumerable<BaseIndexProvider> providers = null)
{
var packagingService = ApplicationContext.Current.Services.PackagingService;
XElement xml;
string type;
if (entity is IContent)
{
type = "content";
xml = packagingService.Export((entity as IContent), false, false);
}
else if (entity is IMedia)
{
type = "media";
xml = packagingService.Export((entity as IMedia), false, false);
}
else
{
type = "member";
// Seems like there is a glitch in the interface :P
// The IMember signature is only available on the concrete implementation of IPackagingService.
xml = ((PackagingService)packagingService).Export((entity as IMember));
}
if (providers != null)
{
ExamineManager.Instance.ReIndexNode(xml, type, providers);
return;
}
ExamineManager.Instance.ReIndexNode(xml, type);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment