Friday, November 20, 2009

SharePoint Search and HTML Meta tags

Using the standard HTML meta tags in your pages (including SharePoint ones) – e.g. Title, Keywords, Description, Robots, etc is quite common. Apart from these you can use custom meta tags (specifying custom values in the name attribute) to define (or extend) your custom meta data for the page. These can be crawled by search engines including the SharePoint search engine and can be used for various filtering and sorting purposes when using the search functionality. Normally in SharePoint the list item (publishing pages are list items) meta data is stored in the list items’ fields but there are cases in which storing meta data in HTML meta tags may have some advantages.

First off – HTML meta tags can be created relatively easy – you can place a custom control in your master page (pages) which outputs the meta tags in the HTML of your pages, an even better solution here is to use a delegate control. So, basically with a single control you ensure that all your pages contain your set of custom meta tags. A particularly good scenario for the meta tags created in this way is when you need them for meta properties whose values can be easily generated or calculated based on other meta data (like list fields) or certain criteria. For example these can be the URLs of the pages or the type of the parent web, etc. In the case of such calculable meta properties you can imagine the overhead of creating auxiliary list fields and maintaining them in all your page libraries compared to the neat approach of the custom web control that generates all meta data in a single piece of code.

A recent example of using meta tags with SharePoint search that I had was for a solution where I needed to display the rollup image of every page in the search results. The problem with the publishing rollup image (actually with the publishing image field type) is that it stores its value as HTML markup (a single HTML img element with several attributes) and the SharePoint crawler simply ignores all HTML markup. The result is that you have your image field containing the source of the image file but the search service just can’t get it for you. A possible solution that I have seen for this problem is to create an additional field that will be populated with just the path of the image file using a list item event receiver for updating it when the rollup image field gets changed. The thing is that implementing and maintaining this is not so easy and not that economically looking.

Here is a small code snippet to demonstrate a simple way to generate the meta tag HTML for the standard rollup image source attribute:

        protected override void OnLoad(EventArgs e)

        {

            base.OnLoad(e);

 

            string image = GetItemRollupImage();

            this.litMeta.Text = string.Format("<meta name=\"myrollupimage\" content=\"{0}\" />", Server.HtmlEncode(image));

        }

 

        protected string GetItemRollupImage()

        {

            if (SPContext.Current.ListItem != null)

            {

                ImageFieldValue imgValue = SPContext.Current.ListItem[FieldId.RollupImage] as ImageFieldValue;

                if (imgValue != null) return imgValue.ImageUrl;

            }

            return string.Empty;

        }

The crawl properties that the SharePoint search crawler creates for the custom HTML meta tags are in the Web crawled properties category – they are with the same name as the name of the custom meta tags – just uppercase. These can be created with code too – the ID of the propset for the meta tag crawled properties is d1b5d3f0-c0b3-11cf-9a92-00a0c908dbf1 (this is actually the ID of the meta tag Web crawled properties subcategory). The last step before you can use the meta tag crawled properties is to create managed properties mapped to them.

3 comments:

  1. Thanks man for explaining very nicely. Do you have any idea how can i implement same thing using JQery and Ajax to avoid page postback?

    ReplyDelete
  2. Hi Varun,
    I am not sure whether I understand you correctly - you want to set the HTML meta tags without using server side code and using JQuery and Ajax instead? Basically, you can fetch the same SharePoint data calling the standard SharePoint web services with ajax, but the problem is that you cannot expose these meta tags to the SharePoint search crawler. It will load your pages but won't execute any javascript in them (and hence no ajax calls), so I am afraid that the only way to set the meta tags is with server side code.

    ReplyDelete
  3. Nice post about meta tags and I think the custom meta tags are valuable. Feeling good and thinking some thing new after reading your blog...

    ReplyDelete