Skip to content

Instantly share code, notes, and snippets.

@lowedown
Last active August 29, 2015 14:01
Show Gist options
  • Save lowedown/53a111bbb8bcf740104a to your computer and use it in GitHub Desktop.
Save lowedown/53a111bbb8bcf740104a to your computer and use it in GitHub Desktop.
Sitecore admin page that checks the resolved Context Index for a specific item.
<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="false"%>
<%@ Import Namespace="Sitecore.ContentSearch" %>
<%@ Import Namespace="Sitecore.Data" %>
<%@ Import Namespace="Sitecore.Data.Items" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Applied Index Checker</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Applied Index Checker</h1>
<p>Returns the name of the index used by sitecore when performing search queries in Sitecore backend. This depends on the start item.</p>
<p>GUID / Path of the start item:</p>
<asp:TextBox type="text" ID="startItem" runat="server"/>
<asp:button ID="button" runat="server" text="Check" OnClick="CheckItem"/>
<p><asp:Literal runat="server" ID="responseMessage"/></p>
</div>
</form>
</body>
</html>
<script runat="server">
protected override void OnInit(EventArgs arguments)
{
CheckSecurity(true);
}
protected void CheckSecurity(bool isDeveloperAllowed)
{
if (Sitecore.Context.User.IsAdministrator || (isDeveloperAllowed && this.IsDeveloper)) return;
var site = Sitecore.Context.Site;
if (site != null)
{
base.Response.Redirect(string.Format("{0}?returnUrl={1}", site.LoginPage, HttpUtility.UrlEncode(base.Request.Url.PathAndQuery)));
}
}
private bool IsDeveloper
{
get
{
return User.IsInRole(@"sitecore\developer") || User.IsInRole(@"sitecore\sitecore client developing");
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
responseMessage.Text = "";
}
}
protected void CheckItem(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(startItem.Text))
{
responseMessage.Text = "Please provide a GUID or path";
}
var db = Database.GetDatabase("master");
if (db == null)
{
responseMessage.Text = "Master database not found.";
return;
}
Item theItem;
Guid itemId;
if (Guid.TryParse(startItem.Text, out itemId))
{
theItem = db.GetItem(new ID(itemId));
}
else
{
theItem = db.GetItem(startItem.Text);
}
if (theItem == null)
{
responseMessage.Text = "Invalid item specified.";
return;
}
responseMessage.Text = string.Format("Index used: <strong>{0}</strong>", GetIndexNameForItem(theItem));
}
private string GetIndexNameForItem(Item item)
{
var index = ContentSearchManager.GetIndex(new SitecoreIndexableItem(item));
if (index == null)
{
return "Index not found";
}
return index.Name;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment