When developing a public facing website using the publishing features of MOSS it might be required to emit a ROBOTS meta tag that prevents internet search engines from indexing specific pages.
The standard RobotsMetaTag control included in WSS generates the following tag:
<META NAME=”ROBOTS” CONTENT=”NOHTMLINDEX”>
Unfortunatelly most search engines do not understand the NOHTMLINDEX content value but a value of NOINDEX.
I already posted a solution for this which replaced the RobotsMetaTag control. Here is now a more elegant solution based on ASP.NET Control Adapters. Control Adapters can be used to modify the rendering of a control based on different browsers. But by using the “Default” browser it also allows to modify the default rendering of a control. Thanks to Gael Duhamel for the idea!
Here is the source code of the control adapter which we will use to modify the behavior of the RobotsMetaTag control:
public class RobotsMetaTagControlAdapter : WebControlAdapter
protected override void Render(System.Web.UI.HtmlTextWriter writer)
SPWeb web = SPContext.Current.Web;
if (web.ASPXPageIndexed == false)
writer.Write(“<META NAME=\”ROBOTS\” CONTENT=\”NOHTMLINDEX,NOINDEX\”/>”);
writer.Write(“”); // updated April 11th to fix a WebControls/Controls cast problem.
The code looks very similar to the code I used in my first approach.
To build the adapter you need to create a class library project, add this source code to it, configure the project to sign the assembly and add the resulting DLL to the global assembly cache (GAC).
To bind the control adapter to the RobotsMetaTag control we now need to add a new browser (e.g. SharePointAdapters.browser) file to the App_Browsers directory of your SharePoint web application.
The content of the browser file needs to look similar to this (you need to adjust the assembly name and strong name key based on your specific assembly):
adapterType=”StefanG.ControlAdapters.RobotsMetaTagControlAdapter, RobotsMetaTagControlAdapter, Version=184.108.40.206, Culture=neutral, PublicKeyToken=dcee90dea8c5fab5”
That’s it! After recycling your application pool the RobotsMetaTag control will now render it’s content based on the custom control adapter. No modification to your master pages will be required.