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.