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

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

Posted on February 06, 2012 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

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.

progress-logo

The Progress Team

View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments

Comments are disabled in preview mode.
Topics

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation