Skip to content

Instantly share code, notes, and snippets.

@uniquelau
Created September 9, 2013 14:10
Show Gist options
  • Save uniquelau/6496103 to your computer and use it in GitHub Desktop.
Save uniquelau/6496103 to your computer and use it in GitHub Desktop.
using System;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using uComponents.Core;
using uComponents.Core.uQueryExtensions;
using umbraco.NodeFactory;
using System.Web;
using Examine;
using Umbraco.Core;
using Umbraco.Web;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.web;
/// <summary>
///
/// </summary>
public class SiteIndexing : IApplicationEventHandler
{
// In our site index, we want to store the id on the home node for a given page. This allows us to easily
// support multi-lingual/portfolio sites in the future, as well as providing a convient way to filter out
// non-content nodes (e.g backoffice) when a search is performed.
// Remember to set the index correctly here...
public void OnApplicationInitialized(UmbracoApplication httpApplication, ApplicationContext applicationContext)
{
ExamineManager.Instance.IndexProviderCollection["SiteIndexer"].GatheringNodeData += SiteIndexing_GatheringNodeData;
}
private void SiteIndexing_GatheringNodeData(object sender, IndexingNodeDataEventArgs e)
{
try
{
Log.Add(LogTypes.Custom, -1, "Attempting to add extra index fields for node " + e.NodeId);
Node indexingNode = TryGetNode(e.NodeId);
if (indexingNode != null)
{
Log.Add(LogTypes.Custom, -1, "Node " + e.NodeId + " found");
// Get the public Home node
Node homeNode = indexingNode.GetAncestorNodes().Where(node => node.NodeTypeAlias == "Home").FirstOrDefault();
// Add fields to index, and populate with id.
e.Fields.Add("homepageNodeId", homeNode.Id.ToString());
}
Log.Add(LogTypes.Custom, -1, "Node " + e.NodeId + " not found");
}
catch (Exception ex)
{
Log.Add(LogTypes.Error, -1, ex.ToString());
}
}
private static Node TryGetNode(int nodeId)
{
try
{
return umbraco.uQuery.GetNode(nodeId);
}
catch
{
return null;
}
}
public void OnApplicationStarted(UmbracoApplication httpApplication, ApplicationContext applicationContext)
{
throw new NotImplementedException();
}
public void OnApplicationStarting(UmbracoApplication httpApplication, ApplicationContext applicationContext)
{
throw new NotImplementedException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment