Create a meta description control

This control produces a meta description element for use in the head element of a page, like this:

<meta name="Description" 
    content="Solien Technology is a Microsoft Gold Certified Partner 
    and Microsoft Managed Partner in Southern California." />

The .net framework’s System.Web.UI.HtmlControls.HtmlMeta control renders as a meta tag, and by using Microsoft.SharePoint.WebControls.FormComponent as our base class we gain access to the current SharePoint item.

namespace Solien.SharePoint
{
    /// <summary>
    /// Adds a meta description tag based on the contents of the specified field
    /// </summary>
    public class MetaDescription : Microsoft.SharePoint.WebControls.FormComponent
    {
        private const string fieldNameDefault = "Description";
        /// <summary>
        /// Field to use for description tag's content. Default value is "Description"
        /// </summary>
        [System.ComponentModel.DefaultValue(fieldNameDefault)]
        public string FieldName
        {
            get
            {
                object o = ViewState["FieldName"];
                if (o == null) return fieldNameDefault;
                return (string)o;
            }
            set { ViewState["FieldName"] = value; }
        }

        protected override void OnPreRender(System.EventArgs e)
        {
            base.OnPreRender(e);

            if (Item != null
                && Item.Fields.ContainsField(FieldName)
                && Item[FieldName] != null
                && !(string.IsNullOrEmpty(Item[FieldName].ToString())))
            {
                System.Web.UI.HtmlControls.HtmlMeta meta =
                    new System.Web.UI.HtmlControls.HtmlMeta();
                meta.Name = "Description";
                meta.Content = Item[FieldName].ToString();
                Controls.Add(meta);
            }
        }
    }
}

Note that the FieldName property has a default value of “Description” since that’s the most common usage. Adding the control to a page layout is simple:

<SolienSharePointControls:MetaDescription runat=server />

No matter what your next project’s description is, we’re here to help with application development and SharePoint consulting.


Feedback

# re: Create a meta description control

Hi! I implemented the code but on my list pages, I am getting a server error. I have it listed below. Did you have a similar problem with SP 2007?

Form control does not have ControlMode set.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Microsoft.SharePoint.SPException: Form control does not have ControlMode set.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SPException: Form control does not have ControlMode set.]
Microsoft.SharePoint.WebControls.FormControlHelper.get_ControlMode() +536
Microsoft.SharePoint.WebControls.FormComponent.get_ControlMode() +64
Microsoft.SharePoint.WebControls.FormComponent.OnPreRender(EventArgs e) +90
Remax.Mainstreet.WebControlLibrary.MetaDescription.OnPreRender(EventArgs e) +80
System.Web.UI.Control.PreRenderRecursiveInternal() +108
System.Web.UI.Control.PreRenderRecursiveInternal() +224
System.Web.UI.Control.PreRenderRecursiveInternal() +224
System.Web.UI.Control.PreRenderRecursiveInternal() +224
System.Web.UI.Control.PreRenderRecursiveInternal() +224
System.Web.UI.Control.PreRenderRecursiveInternal() +224
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3394
6/1/2010 6:57 AM | Jeanna Bash

Post a comment





 

Please add 7 and 3 and type the answer here: