Skip to content

Instantly share code, notes, and snippets.

@BillCacy
BillCacy / PageContentField.cs
Last active October 19, 2015 16:04
Computed Index field for parsing page content and stripping out html, comments, scripts, etc..
using System;
using System.Linq;
using System.Net;
using System.Text;
using HtmlAgilityPack;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.ComputedFields;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
@BillCacy
BillCacy / SiteSearchIndex.Web.config
Created September 17, 2015 22:03
Sitecore site search custom index, based off of the default web index.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<contentSearch>
<configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
<indexes hint="list:AddIndex">
<index id="sitesearch_web_index" type="Sitecore.ContentSearch.LuceneProvider.LuceneIndex, Sitecore.ContentSearch.LuceneProvider">
<param desc="name">$(id)</param>
<param desc="folder">$(id)</param>
<!-- This initializes index property store. Id has to be set to the index id -->
<param desc="propertyStore" ref="contentSearch/indexConfigurations/databasePropertyStore" param1="$(id)" />
@BillCacy
BillCacy / SiteSearchIndex.WebConfiguration.config
Created September 17, 2015 22:11
Configuration for the web site search.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<contentSearch>
<indexConfigurations>
<sitesearchWebConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
<indexAllFields>false</indexAllFields>
<initializeOnAdd>true</initializeOnAdd>
<analyzer ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration/analyzer" />
<fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch">
<fieldNames hint="raw:AddFieldByFieldName">
@BillCacy
BillCacy / SiteSearchResult.cs
Created September 17, 2015 22:24
Result class for mapping from the search interface
namespace YOURNAMESPACE.Search
{
public class SiteSearchResult
{
public string Title { get;set; }
public string PageLinkUrl { get; set; }
public string Content { get; set; }
}
}
@BillCacy
BillCacy / PageBaseSearchResult.cs
Created September 17, 2015 22:27
SearchResult implementation for use with the search api.
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.SearchTypes;
namespace YOURNAMESPACE.ConcreteModels
{
public class PageBaseSearchResult : SearchResultItem
{
[IndexField("pagecontentfield")]
public string PageContentField{ get; set; }
}
@BillCacy
BillCacy / SearchResultsIndexAgent.cs
Created September 17, 2015 22:43
These are the methods used by the controller to handle the actual searching and mapping of results to the view model.
public void Search(string term, string page)
{
if (string.IsNullOrEmpty(term))
{
ViewModel.SearchTerm = "No search term entered";
return;
}
ViewModel.SearchTerm = term;
var sitecoreService = new SitecoreService(Sitecore.Context.Database);
@BillCacy
BillCacy / SearchResultsViewModel.cs
Created September 17, 2015 22:45
View model used for the search results
using System.Collections.Generic;
namespace YOURNAMESPACE.ViewModels.Common.Components.Search
{
public class SearchResultsViewModel
{
public string ViewPath { get { return Constants.Components.SearchResultsViewPath; } }
public IEnumerable<SiteSearchResult> SearchResults { get; set; }
public int TotalPageCount { get; set; }
public int TotalResults { get; set; }
@BillCacy
BillCacy / SearchResults.cshtml
Created September 17, 2015 22:47
This is the view used to render the search results.
@model YOURNAMESPACE.ViewModels.Common.Components.Search.SearchResultsViewModel
<div class="container-fluid width-90">
<div class="row">
<h1>Search results for &ldquo;@Model.SearchTerm&rdquo;</h1>
@if (Model.SearchResults != null && Model.SearchResults.Any())
{
foreach (var result in Model.SearchResults)
{
@BillCacy
BillCacy / AddLeftNavAndSocialMedia.ps1
Created June 9, 2016 19:02
Sitecore Powershell script to add renderings to Product Pages
$root = gi master:// -id "{076927BD-FFD2-4658-B522-D836C045CF7D}"
$items = $root | ls -r | ?{$_.TemplateName -eq "ProductPage"}
$leftNavRendering = gi master:\layout\Renderings\Site\Components\LeftNav | New-Rendering -Placeholder "contentMain"
$socialMediaRendering = gi master:\layout\Renderings\Site\Components\SocialMedia\SocialMediaBar | New-Rendering -Placeholder "contentMain"
foreach($i in $items){
if($leftNavDsPath){
clear-variable("leftNavDsPath")
}
if($leftNavDs){
@BillCacy
BillCacy / UpdateFieldValues.ps1
Created June 9, 2016 19:03
Sitecore Powershell - Update field values in all rich text items
$root = gi master:// -id "{0C3015B8-77AD-44DC-9633-2AD76489483A}"
$items = $root | ls -r | %{$item = $_; $_.Fields | ?{$_.TypeKey -eq "rich text"} }
#$items.Count
foreach($i in $items){
if($temp){
clear-variable("temp")
}
if($replaced){
clear-variable("replaced")
}