Skip to content

Instantly share code, notes, and snippets.

@RyanABailey
RyanABailey / Item Resolver
Created April 11, 2016 07:50
Sitecore Custom Item Resolver
public class MyCustomResolver : HttpRequestProcessor
{
public override void Process(HttpRequestArgs args)
{
// Return if Sitecore has found the item
if (Context.Item != null || Context.Database == null || args.Url.ItemPath.Length == 0) return;
// If item not found
var itemPath = args.Url.ItemPath; // The requested item path
@RyanABailey
RyanABailey / C# Class
Last active April 4, 2016 07:20
Custom workflow action
using Sitecore.Workflows.Simple;
namespace MyProject.Workflows
{
public class CustomWorkflowAction
{
public void Process(WorkflowPipelineArgs args)
{
var workflowItem = args.DataItem; // The item in the workflow
// Custom logic here
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<contentSearch>
<configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
<indexes hint="list:AddIndex">
<index id="MySearchIndex" type="Sitecore.ContentSearch.LuceneProvider.SwitchOnRebuildLuceneIndex, Sitecore.ContentSearch.LuceneProvider">
<strategies hint="list:AddStrategy">
<!-- NOTE: order of these is controls the execution order -->
<strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/intervalAsync3Hour" />
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<contentSearch>
<indexConfigurations>
<indexUpdateStrategies>
<intervalAsync3Hour type="Sitecore.ContentSearch.Maintenance.Strategies.IntervalAsynchronousStrategy, Sitecore.ContentSearch">
<param desc="database">web</param>
<param desc="interval">03:00:00</param>
<CheckForThreshold>true</CheckForThreshold>
</intervalAsync3Hour>
@RyanABailey
RyanABailey / Computed Index Field
Created March 16, 2016 20:26
Sitecore Lucene index datasource
namespace MyProject
{
/// <summary>
/// Index rendering/sublayout datasource
/// </summary>
public class IndexDataSource : IComputedIndexField
{
/// <inheritdoc />
public string FieldName { get; set; }
/// <inheritdoc />
private static Expression<Func<SearchModel, bool>> GetCategoryFacetPredicate(List<string> categories)
{
var predicate = PredicateBuilder.True<SearchModel>(); // Items which meet the predicate
foreach (var category in categories)
{
predicate = predicate.Or(x => x.Category == category);
}
return predicate;
@RyanABailey
RyanABailey / Search code
Last active March 14, 2016 23:46
Sitecore Lucene Facet
var searchIndex = ContentSearchManager.GetIndex("MySearchIndex"); // Get the search index
var searchPredicate = BuildSearchPredicate(searchRequest); // Build the search predicate
using (var searchContext = searchIndex.CreateSearchContext()) // Get a context of the search index
{
var searchResults = searchContext.GetQueryable<SearchModel>().Where(searchPredicate); // Search the index for items which match the predicate
var searchFacets = searchContext.GetQueryable<SearchModel>().Where(searchPredicate).FacetOn(x => x.Category).GetFacets();
// Category facets
@RyanABailey
RyanABailey / Computed Index Field
Last active March 14, 2016 23:38
Lucene index category
namespace MyProject.ComputedIndexFields
{
/// <summary>
/// Index the main level section the item falls under
/// </summary>
public class IndexCategory : IComputedIndexField
{
/// <inheritdoc />
public string FieldName { get; set; }
/// <inheritdoc />
@RyanABailey
RyanABailey / C#
Created March 10, 2016 21:36
Lucene best matching content
/// <summary>
/// Checks a list of content for the best match for a search term
/// </summary>
/// <param name="searchTerm">search term</param>
/// <param name="searchContent">search content</param>
/// <returns>Best match</returns>
private static string GetBestMatch(string searchTerm, List<string> searchContent)
{
// create analyzer
var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
@RyanABailey
RyanABailey / C#
Created March 10, 2016 21:03
Sitecore search context highlighting
/// <summary>
/// Highlight search term
/// </summary>
/// <param name="searchTerm">Search term</param>
/// <param name="searchContent">search content</param>
/// <returns>Search content with highlighted search term</returns>
private static string HighlightSearchTerm(string searchTerm, string searchContent)
{
// create analyzer
var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);