Triggering an Index Update for the Linked Items in SITECORE
Overview
In some scenarios
you may need to include child items into the search document of the parent item.
In such cases, if any of the child items is changed (edited\removed) we will
have to maintain "relationship" to the linked item (parent item) in
the index manually, and this require some coding.
Example
Let’s say we have
a computed field attached to the home page, this computed field should contain
the value for the “Title” field on “Child Item” and we need that the computed field
to be updated once we edit\remove the child item by refreshing the index.
Solutions
Here we have two
methods to refresh the index:
1.
indexing.getDependencies Pipeline
This
pipeline is designed to address issues when a search document is built from the
data coming from more than one item. Each custom processor must implement Sitecore.ContentSearch.Pipelines.GetDependencies
BaseProcessor
Below is a sample
code how we can implement this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GetServiceDependencies : BaseProcessor | |
{ | |
public override void Process(GetDependenciesArgs context) | |
{ | |
Assert.IsNotNull(context.IndexedItem, "indexed item"); | |
Assert.IsNotNull(context.Dependencies, "dependencies"); | |
var scIndexable = context.IndexedItem as SitecoreIndexableItem; | |
if (scIndexable == null) return; | |
var item = scIndexable.Item; | |
if (item == null) return; | |
if (Sitecore.Context.Job == null) return; | |
if (item.TemplateID == new Sitecore.Data.ID("{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"))// template ID for the child item | |
{ | |
var dependency = item.Parent; // Home page | |
if (dependency.TemplateID == new Sitecore.Data.ID ("{B0CE3D12-D662-47A9-8BC6-E9AE36C6E22F}"))//Template ID for the home page | |
{ | |
var id = (SitecoreItemUniqueId)dependency.Uri; | |
if (!context.Dependencies.Contains(id)) | |
{ | |
context.Dependencies.Add(id); | |
} | |
} | |
} | |
} | |
} |
We need to add the
below patch:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"> | |
<sitecore> | |
<pipelines> | |
<indexing.getDependencies help="Processors should derive from Sitecore.ContentSearch.Pipelines.GetDependencies.BaseProcessor"> | |
<processor type="YourNamespace.GetServiceDependencies, YourAssembly"/> | |
</indexing.getDependencies> | |
</pipelines> | |
</sitecore> | |
</configuration> |
2.
Marking the linked item as changed
To mark a linked item as changed, we
need just to save it via API (e.g. via custom “item:deleted” handler, that will
fire on each delete of the child item). This action will force changing value
in the system Revision field, which means that all publishing types will treat
this "item change" as the "one that needs to be published".
When the parent item is published, it will be handled by the default indexing
mechanism, so no further coding is required.
Below is a sample
code how we can implement this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IndexUpdateOnItemDeleteHandler | |
{ | |
public void OnItemDeleted(object sender, EventArgs args) | |
{ | |
Item item = Event.ExtractParameter(args, 0) as Item; | |
ID parentId = Event.ExtractParameter(args, 1) as ID; | |
if (item != null && (item.TemplateID == new Sitecore.Data.ID("{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}")))// template ID for the child item | |
{ | |
Item parent = item.Database.GetItem(parentId);//Home page | |
if (parent != null && parent.TemplateID == new Sitecore.Data.ID("{B0CE3D12-D662-47A9-8BC6-E9AE36C6E22F}")) | |
{ | |
Item homePage = (SitecoreIndexableItem)parent; | |
using (new SecurityDisabler()) | |
{ | |
homePage.Editing.BeginEdit(); | |
homePage[Constants.FieldIDs.Revision] = new Guid().ToString(); | |
homePage.Editing.EndEdit(); | |
} | |
} | |
} | |
} | |
} |
We need to add the
below patch:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"> | |
<sitecore> | |
<events> | |
<event name="item:deleted"> | |
<handler type="YourNamespace.IndexUpdateOnItemDeleteHandler,YourAssembly" method="OnItemDeleted"/> | |
</event> | |
</events> | |
</sitecore> | |
</configuration> |
The same mechanism can be applied on
the “item:saved” handler to handle updating the item fields.
References:
0 comments:
Post a Comment