Tag Archives: c#

EPiServer 7/Search: Prevent content from being indexed

Let’s say you have a page or a page type that you do not want to get indexed by the EPiServer Search service. The solution is to implement the ISearchable interface:

bool AllowReIndexChildren { get; }
bool IsSearchable { get; }

It requires you to implement two boolean properties, AllowReIndexChildren and IsSearchable. By letting IsSearchable return false you’ll prevent the page from being indexed. You could either implement it as a page property, having the editor choose which pages should be excluded:

[Display(Name = "Exclude page from site search",
    Description = "Check this box to prevent page from being indexed by the site's search engine.",
    GroupName = SystemTabNames.Settings,
    Order = 100)]
public virtual bool IsSearchable
        bool isSearchable = this.GetPropertyValue(p => p.IsSearchable);
        return !isSearchable;
        this.SetPropertyValue(p => p.IsSearchable, value);

Or you could prevent the the entire page type from being indexed:

[ContentType(DisplayName = "Sneaky page that won't get indexed.", GUID = "00000000-0000-0000-0000-000000000000")]
public class SneakyPage : PageData, ISearchable
    public bool AllowReIndexChildren { get { return true; } }
    public bool IsSearchable { get { return false; } }

But what about content that is already in the index? Ticking the box to prevent indexing on a page that has previouly been index will only prevent updates to this item, not prevent it from showing in search results.

In order to make sure indexed items are removed add the code below to Global.asax

protected void Application_Start()
    DataFactory.Instance.PublishedContent += OnPublishedContent;

private void OnPublishedContent(object sender, ContentEventArgs contentEventArgs)
    ISearchable seachable = contentEventArgs.Content as ISearchable;
    if (seachable == null) {

    if (seachable.IsSearchable) {

    CultureInfo language = null;
    ILocalizable localizable = contentEventArgs.Content as ILocalizable;

    if (localizable != null) {
        language = localizable.Language;

    string searchId = string.Concat(contentEventArgs.Content.ContentGuid, "|", language);
    IndexRequestItem indexItem = new IndexRequestItem(searchId, IndexAction.Remove);

Html Agility Pack: Get all heading tags

A project I’m currently working on involves parsing a lot of HTML using the Html agility pack. One problem I came across was finding all heading level tags between 1 and 6 and here’s my solution using Xpath:

string xpathQuery = "//*[starts-with(name(),'h') and string-length(name()) = 2 and number(substring(name(), 2)) <= 6]";
var tags = htmlDocument.DocumentNode.SelectNodes(xpathQuery);

If someone has a better or alternative solution please let me know in the comments.