Skip to content

Instantly share code, notes, and snippets.

@KKings
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KKings/e84ea06fd05aeee356ab to your computer and use it in GitHub Desktop.
Save KKings/e84ea06fd05aeee356ab to your computer and use it in GitHub Desktop.
Page Editor warning for duplicate item names
namespace NavArts.Insights.Pipelines.GetPageEditorNotifications
{
using System;
using System.Linq;
using global::Sitecore.ContentSearch.Fluent;
using global::Sitecore.ContentSearch.Utilities;
using global::Sitecore.Data;
using global::Sitecore.Data.Items;
using global::Sitecore.Diagnostics;
using global::Sitecore.Pipelines.GetPageEditorNotifications;
using Search.Results;
using Sitecore.Extensions;
using SC = global::Sitecore;
/// <summary>
/// Uses an index to query for items with a name match (exact)
/// </summary>
public class DuplicateItemNameValidatorWarning : GetPageEditorNotificationsProcessor
{
/// <summary>
/// Template Id to Specify the type of Content Item to Run on and to filter out search results
/// </summary>
private readonly ID _templateId;
/// <summary>
/// Index Used for searching based on name
/// </summary>
private const string IndexName = "sitecore_master_index";
/// <summary>
/// Instantiates a new instance of the <see cref="DuplicateItemNameValidatorWarning"/> with a template id
/// </summary>
public DuplicateItemNameValidatorWarning(string templateId)
{
Assert.IsNotNullOrEmpty(templateId, "templateId cannot be null.");
this._templateId = ID.Parse(templateId);
}
/// <summary>
/// Query the index for any matching items with the same name
/// </summary>
/// <param name="args">Page Editor Args</param>
public override void Process(GetPageEditorNotificationsArgs args)
{
var item = args.ContextItem;
if (item == null || item.TemplateID != this._templateId)
{
return;
}
var itemId = item.ID.ToString().NormalizeGuid();
var results =
new SearchManager(DuplicateItemNameValidatorWarning.IndexName, DuplicateItemNameValidatorWarning.IndexName).ResultsFor<InsightsResultItem>
(search => search
.Options(o => o
.AddRestriction(this._templateId))
.Query(query => query
.And(and => and
.Where(result => result.Name == item.Name)))
.Filter(filter => filter
.And(and => and
.Where(result => result.RawId != itemId))));
if (results.Total > 0)
{
var description = SC.Globalization.Translate.Text(
"If you publish now, the current item may not render properly because the following items have the same name. Click the links below to review these items:");
var editorNotification = new PageEditorNotification(description, PageEditorNotificationType.Warning);
results.Results.Select(result => item.Database.GetItem(result.Id)).ForEach(i => DuplicateItemNameValidatorWarning.AddWarningOption(i, editorNotification));
args.Notifications.Add(editorNotification);
}
}
/// <summary>
/// Add a warning option to the GetPageEditorNotificationsArgs
/// </summary>
/// <param name="dataSourceItem">Item to reference</param>
/// <param name="pageEditorNotification">Page Editor Notification</param>
private static void AddWarningOption(Item dataSourceItem, PageEditorNotification pageEditorNotification)
{
pageEditorNotification.Options.Add(new PageEditorNotificationOption(dataSourceItem.Name,
String.Format("webedit:open(id={0},language={1},version={2})",
dataSourceItem.ID,
dataSourceItem.Language,
dataSourceItem.Version.Number)));
}
}
}
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<!--
Page Editor Warnings
-->
<getPageEditorNotifications>
<!--
Adds Page Editor Warnings if an Item has the same Item Name as other Items of the same type
-->
<processor type="NavArts.Insights.Pipelines.GetPageEditorNotifications.DuplicateItemNameValidatorWarning, NavArts.Insights">
<param desc="templateId">{96b9ce40-cd8a-4bd5-a523-028e5ef8632e}</param>
</processor>
</getPageEditorNotifications>
</pipelines>
</sitecore>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment