The new search function in Relate+ 2.0 is really powerful. Relate+ will populate the search index with community content, CMS pages and files out-of-the-box. However the search page that is included in the templates will only search community content.
Eric Pettersson has blogged about adding CMS page search to the search page of Relate+ 2.0 – this is great! However he left out a few steps that you have to take to make the search work completely. In this blog post I will highlight these steps. I will assume that you have read Eric’s post and implemented that part already.
If you have followed Eric’s steps and then played around with your site, the chances are good that your index is a bit off – especially if you have deleted pages. Because of this, the first thing that we want to do is re-build the index. This is done using this code snippet:
EPiServer.Search.SearchHandler.Instance.ResetIndex("default");
EPiServer.Community.Search.SearchHandler.Instance.AddCommunityEntitiesToIndex(true);
EPiServer.Templates.RelatePlus.CmsModules.CmsSearchHandler.Instance.IndexPublishedCmsPages();
The “default” is the name of your index, change it if you have changed the name of the index. You can find that code in the attached ReIndexRelatePlus2.aspx – this is a modified version of the re-index page found here. Simply place the file in your web root and run it.
Now locate the file CmsIntegrationModule.cs in /Templates/RelatePlus/InitializationModules/and have a look at the method called Instance_MovedPage. We need to modify this version so that the search index is always updated. In the code that you get out-of-the-box the search index will only be updated if the Club association has been effected. Change the code to something like this:
private static void Instance_MovedPage(object sender, PageEventArgs e)
{
// Guard
if (e == null || PageReference.IsNullOrEmpty(e.PageLink)) return;
// Remove the page from cache due to Bug #24524 where IsDeleted gets its value from cache.
DataFactoryCache.RemovePage(e.PageLink);
PageData currentPage = EPiServer.DataFactory.Instance.GetPage(e.PageLink);
// Update this page in search index
// Since we don't want pages from the recycle bin in search result, we remove them from index
if (currentPage.IsDeleted)
{
CmsSearchHandler.Instance.UpdateIndex(currentPage, IndexAction.Remove);
}
else
{
CmsSearchHandler.Instance.UpdateIndex(currentPage, IndexAction.Update);
}
// Get the club id
int clubId = GetClubId(e.PageLink);
if (clubId != -1)
{
// Get the club
Club associatedClub = ClubHandler.Instance.GetClub(clubId);
if (associatedClub != null)
{
Club clubClone = associatedClub.Clone() as Club;
if (currentPage.IsDeleted)
{
clubClone.RemoveAssociatedPage(currentPage);
ClubHandler.Instance.UpdateClub(clubClone);
}
}
}
}
In this code I have also changed it so that when a page is deleted, i.e. moved to the recycle bin, the page is removed from the search index. This is done since we do not want to include deleted pages in the search result.
You can find the entire source code here. Enjoy!
Disclaimer: Please note that the code comes as-is and I will not be held responsible.