Skip Ribbon Commands
Skip to main content

Point2Share | Daniel McPherson's SharePoint Blog

:

The zevenseas Community > Blogs > Point2Share | Daniel McPherson's SharePoint Blog > Posts > Using HTML Meta Tags to add Information to SharePoint Search
December 07
Using HTML Meta Tags to add Information to SharePoint Search

This week Robin was working away on a tool to make it easier for people to find Team Sites across their organisation. He was evaluating the use of SharePoint Search to do it, but found that many site properties, for example the template that Publishing Sites are based on is not marked as STS_Site, were not exposed in the index.

This reminded me of a very similar exploration I did a little while ago, and it reminded me that I didn’t blog about it and should have. So now I am.

As many of you know, SharePoint is very good at automatically finding metadata as it crawls content. It finds document properties, list columns, and much more. It collects these all together and adds them into the SSP as “Crawled Properties”. Once in this list, you can map them to a “Managed Property”, thereby making them available for use in search queries and results.

A little known fact is that SharePoint can also pick up properties from HTML pages via the “META” tag. For example, in the top of the home page on any SharePoint site you will see:

<META Name="CollaborationServer" Content="SharePoint Team Web Site">

This value is detected by SharePoint and then added to the Crawled Properties here:

image

This got me thinking. Wouldn’t this make it possible to create a small control that used the SharePoint OM to pull out the properties from the site and then expose them inside a META Tag. Once there would be picked up by the Indexer? The answer course was ‘Yes’. Even better, you could add this control onto any site using the “Delegate Control” capability.

Here is the little base class I built:

using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
 
namespace codezeven.Social.WebControls
{
    public class MetaManager : Control
    {
        private Dictionary<string, string> metaData;
        protected SPWeb currentWeb;
 
        public MetaPropPageType PageType
        {
            get
            {
                if (IsDefaultPage())
                    return MetaPropPageType.Default;
 
                return MetaPropPageType.None;
            }
        }
 
        public Dictionary<string, string> MetaData
        {
            get { return metaData; }
            set { metaData = value; }
        }
 
        protected override void OnInit(System.EventArgs e)
        {
            this.currentWeb = SPContext.Current.Web;
            metaData = new Dictionary<string, string>();
        }
 
        protected override void CreateChildControls()
        {
            foreach (KeyValuePair<string, string> pair in metaData)
            {
                HtmlMeta metaTag = new HtmlMeta();
                metaTag.Name = pair.Key.ToUpper();
                metaTag.Content = pair.Value;
                Controls.Add(metaTag);
            }
            
            base.CreateChildControls();
        }
 
        protected bool IsDefaultPage()
        {
            string defaultPage = currentWeb.Url.TrimEnd('/') + "/default.aspx";
            string currentPage = HttpContext.Current.Request.Url.OriginalString;
 
            if (!string.IsNullOrEmpty(defaultPage) && !string.IsNullOrEmpty(currentPage))
            {
                if (currentPage.ToLower() == defaultPage.ToLower())
                    return true;
            }
 
            return false;
        }
    }
}


And the referenced Enum:

namespace codezeven.Social.WebControls
{
    public enum MetaPropPageType
    {
        Default, None        
    }
}

From here you can just layer your own control on top, to output just the values you would like from SharePoint:

using System;
using codezeven.Social.WebControls;
 
namespace codezeven.Social.WebControls
{
    public class SiteMetaManager : MetaManager
    {
        protected override void CreateChildControls()
        {
            MetaData.Add("SharePointWebTitle", currentWeb.Title);
 
            switch (PageType)
            {
                case MetaPropPageType.Default:
                    MetaData.Add("SharePointWebTemplate", currentWeb.WebTemplate);
                    MetaData.Add("SharePointWebDescription", currentWeb.Description);
                    break;
                case MetaPropPageType.None:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
 
           base.CreateChildControls();
        }
    }
}

 

Then via a feature add it in as a Delegate control with something like this:

<Control Id="AdditionalPageHead" 
Sequence="50" 
ControlClass="codezeven.Social.WebControls.BlogMetaManager"
ControlAssembly="codezeven.Social, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=0fec8ae58e85e93e" />

 

This will then produce something like the following in the top of each page (ok, so my demo data is not ideal! <grin>):

<meta name="SHAREPOINTWEBTITLE" content="Setup's Again" />
<meta name="SHAREPOINTWEBTEMPLATE" content="BLOG" />
<meta name="SHAREPOINTWEBDESCRIPTION" content="This is a really long description just to see if it comes across ok." />


And after performing a Full Crawl, you will find the following new “Crawled Properties”:

image

After mapping them to equivalent Managed Properties, and using our useful SearchCoder tool, you can see the result here:

image

image

There is a downside, these properties only change in the Index after a Full Crawl (or when the page changes, though never really bottomed that out as it lost its real benefit at that point). This was a deal breaker for me, but no problem for Robin. Anyway, that’s it, hope it helps!

Comments

Anonymous

Really great article.

It's a really nice workaroung to the SharePoint search when you don't want to crawl list / dispform / editform. I set it up in my aspx page and I can now have custom tag. Thanks!
System Account on 11/02/2010 06:50

Sohbet

It's a really nice workaroung to the SharePoint search when you don't want to crawl list / dispform / editform. I set it up in my aspx page and I can now have custom tag. Thanks!
System Account on 28/03/2010 01:57
 

 Statistics

 
Views: 2890
Comments: 2
Tags:
Published:1599 Days Ago