Search code examples
asp.netsharepointcontrol-adapter

Hiding a 3rd party control from search crawler using a browser file not working as expected


I'm using in asp.net application a 3 party web control that requires some browser features (javascript especially).

The control is checking if the browser has the required capabilities or throw an exception if not.

This is OK when navigating to a page that uses this control, but when a robot is indexing the page, an error is thrown (because the user agent of the crawler is not giving the required capabilities).

As I cannot control how/when the control is instantiated, I was trying to short circuit the control to avoid the check. To reach this, I've create a custom browser file, that I've put in APP_Browsers folder:

<?xml version="1.0" encoding="utf-8"?>
<browsers>
    <browser refID="Default">
        <controlAdapters>
            <adapter controlType="Microsoft.SharePoint.WebControls.Gantt, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
                     adapterType="myNS.MyAdapter, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c02e9cbf6dab9cb0" />
        </controlAdapters>
    </browser>
</browsers>

(Yes this is a sharepoint control, but I suppose it's out of scope)

And here is the code of the adapter:

namespace myNS
{
    public class MyAdapter: ControlAdapter
    {
        protected override void OnLoad(EventArgs e)
        {
            System.Diagnostics.Debugger.Break();
            var context = HttpContext.Current;
            if (context != null)
            {
                var browser = context.Request.Browser;
                if (!browser.Crawler)
                {
                    base.OnLoad(e); // 
                }
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            System.Diagnostics.Debugger.Break();
            var context = HttpContext.Current;
            if (context != null)
            {
                var browser = context.Request.Browser;
                if (!browser.Crawler)
                {
                    this.Control.RenderControl(writer);
                }
            }
        }
    }
}

Unfortunately, this does not works. I don't have an error message, but the code seems to be never called (the Break() method is not firing expecter debugger window). Manually attaching the debugger is not better.

How can I ensure my browser file is actually used, and correct?


Solution

  • Hum... not sure to understand, but as soon as I modified "compat.browser" file (not mine), it started to works.

    strange