+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Hiding Page Content From The Search Engine In Sitefinity Using Layout Widgets

by Ivan Pelovski

This post will describe how to create a layout widget that hides its content from the search engine in Sitefinity. This type of layout widget is useful if you want to hide a content block from the indexing or you don't want to write the same control logic for every widget you develop. Writing this layout widget is pretty simple. All that has to be done is to extend the built-in control LayoutControl and reuse the layout templates.

So let's start by adding a new class to your SitefinityWebApp project. Give it a proper name (in this sample it is called NonSearchableLayout) and make it to extend the LayoutControl.

public class NonSearchableLayout : Telerik.Sitefinity.Web.UI.LayoutControl

Next we'll override two of the methods of System.Web.UI.Control class: 

protected override void OnInit(EventArgs e)
{
    this.Visible = this.GetIndexRenderMode() == Telerik.Sitefinity.Web.UI.IndexRenderModes.Normal;
}

The GetIndexRenderMode() method is an extension method. It says whether the page is rendered by the search engine or it is rendered normally. Setting the Visible property of the control to False makes the control to skip the work it usually does when it is visible. It even skips calling the Render method but for any case we'll override the Render method too. 

protected override void Render(HtmlTextWriter writer)
{
    if (this.GetIndexRenderMode() == Telerik.Sitefinity.Web.UI.IndexRenderModes.Normal)
    {
        base.Render(writer);
    }
}
And here is a complete listing of the code (I put it for the sake of completeness):

using System;
using System.Web.UI;
namespace SitefinityWebApp.Code
{
    public class NonSearchableLayout : Telerik.Sitefinity.Web.UI.LayoutControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.Visible = this.GetIndexRenderMode() == Telerik.Sitefinity.Web.UI.IndexRenderModes.Normal;
        }
        // Just in case. Should not be called in normal cases if the control has its property Visible set to False
        protected override void Render(HtmlTextWriter writer)
        {
            if (this.GetIndexRenderMode() == Telerik.Sitefinity.Web.UI.IndexRenderModes.Normal)
            {
                base.Render(writer);
            }
        }
    }
}

So this is it. Build the SitefinityWebApp project and the layout widget is ready to be registered in Sitefinity. The documentation page for registering custom layout widgets is here, but here is what to be done:

  1. In the main menu, click Administration » Settings.
    The Basic settings page appears.
  2. Click Advanced.
  3. Expand Toolboxes » Toolboxes » PageLayouts » Sections » TwoColumns » Tools.
  4. Click Create new.
  5. Fill the form in the following way:
    • In ControlType enter "SitefinityWebApp.Code.NonSearchableLayout".
    • In the Name field enter "NonSearchableLayout".
    • In both Title and Description fields enter "Non searchable layout".
    • In CssClass field enter "sfL100".
    • In LayoutTemplate filed enter the following path "~/SFRes/Telerik.Sitefinity.Resources.Templates.Layouts.Column1Template.ascx" (this is the template of the "100%" layout also known as "Single column layout" :) ).
  6. Click Save changes.

Now you can edit one of your pages and give a try to the new layout widget.

I want to add a note about the usage of this layout. You may ask do you need such layout widget for the other template types ("Two columns with 50% width each.", etc.) The good thing is that you can use the other layouts with this new one in various combinations. For example you don't want the content inside one of your layouts to be indexed. Simply drop a non searchable layout and move the other layout inside. Or you may want the content inside the left part of one of your layouts not to be indexed. In this case drop the new layout in the left part of the existing layout and move its content to the new layout. You can even position a layout widget among other widgets (Content Blocks, News Widget, etc) so you can exclude a certain widget from the search and not exclude its neighbor widgets. 

The layout widgets of Sitefinity are a really powerful instrument that can be used for purposes not only related to styling. In this case we used them to change the behavior of other widgets in different scenarios.

If you have a question or want to make a suggestion please leave a comment.

9 comments

Leave a comment
  1. Robert May 25, 2012
    This could be the solution I am looking for.

    My scenario: I put the LoginName widget in my page template and I observed that all my site search result shows the administrator name (being myself as the administrator). The reindex action could have pickup the "internal" rendering of all pages and pickup the LoginName value.
    Perhaps this could solve my problem.
  2. Robert May 25, 2012
    This could be the solution I am looking for.

    My scenario: I put the LoginName widget in my page template and I observed that all my site search result shows the administrator name (being myself as the administrator). The reindex action could have pickup the "internal" rendering of all pages (LoginName widget gets the logged in value of the administrator name) and became part of the search result.

    Perhaps this could solve my problem. Thanks Ivan.
  3. Robert May 28, 2012
    When I followed this code, I get something like "Resource not found.." then I changed
    "SitefinityWebApp.Code.NonSearchableLayout" to "SitefinityWebApp.NonSearchableLayout" and it works!

    The Site search should also be reindexed  to check if the NonSearchableLayout is working. :)
  4. Robert May 29, 2012
    I received the following when the site gets uploaded online.

    Post cache substitution is not compatible with modules in the IIS integrated pipeline that modify the response buffers.  Either a native module in the pipeline has modified an HTTP_DATA_CHUNK structure associated with a managed post cache substitution callback, or a managed filter has modified the response.

    In my local machine, its working fine. I am using the Sitefinity Project Manager to browse and administer the website.

    Any ideas how to solve this?
  5. Robert May 29, 2012
    I received the following when the site gets uploaded online.

    Post cache substitution is not compatible with modules in the IIS integrated pipeline that modify the response buffers.  Either a native module in the pipeline has modified an HTTP_DATA_CHUNK structure associated with a managed post cache substitution callback, or a managed filter has modified the response.

    In my local machine, its working fine. I am using the Sitefinity Project Manager to browse and administer the website.

    Any ideas how to solve this?
  6. Nikhil Jun 06, 2012
    • (Support ID:534180) posted to telerik people  regarding issue posted like this, I am seeing the HTML script / style information when I search for map and open the links below link to see to the issue
    • (http://homecaretechc16.bandyworks.com/VGR/search-result?indexCatalogue=about-us&searchQuery=map&wordsMode=0) . They suggested to implement non searchable layout and provided us to link for development. After  implementation of layout control still we are facing same issue please help us to how to fix it.

     

  7. Nikhil Jun 06, 2012
    • (Support ID:534180) posted to telerik people  regarding issue posted like this, I am seeing the HTML script / style information when I search for map and open the links below link to see to the issue
    • (http://homecaretechc16.bandyworks.com/VGR/search-result?indexCatalogue=about-us&searchQuery=map&wordsMode=0) . They suggested to implement non searchable layout and provided us to link for development. After  implementation of layout control still we are facing same issue please help us to how to fix it.

     

  8. Aaron Jun 13, 2012

    I am also having this issue.

    Post cache substitution is not compatible with modules in the IIS integrated pipeline that modify the response buffers.  Either a native module in the pipeline has modified an HTTP_DATA_CHUNK structure associated with a managed post cache substitution callback, or a managed filter has modified the response

  9. Charles Feb 26, 2015
    How does one do this in Sitefinity 3.7?

    Leave a comment