One of the common search engine optimization techniques used is adding the META
tags to the HEAD
element of a page to include additional information about the page. These META
tags can include author information, keywords, a description, copyright information, and so on. For sites where this information is the same for every page, we can simply add the META
tags to the master page. However, for sites that require different information for each page, we can use an AdditionalPageHead
control to dynamically add the META
tags.
In this recipe, we will add the META
tags to the HEAD
element of pages that have specific list item fields.
We should have already created our Visual Studio project in the Creating a Visual Studio solution for custom delegate controls recipe of this chapter before starting this recipe. In addition, on the Pages library we are testing this recipe with, we should have added two custom columns: Meta Keywords and Meta Description. For one or more of the pages in the library, we should have set the value for these fields on the properties of the pages.
Follow these steps to add the META
tags with a delegate control:
Controls
folder.public
and inherit from the UserControl
base class:public class CustomMetaTags : UserControl
<meta>
elements as follows:private const string FieldNameKeywords = "Meta Keywords"; private const string FieldNameDescription = "Meta Description"; private const string FormatMetaTagKeywords = "<meta name="keywords" content="{0}" />"; private const string FormatMetaTagDescription = "<meta name="description" content="{0}" />";
CreateChildControls
method and add a monitored scope:protected override void CreateChildControls() { using (new SPMonitoredScope("Code6587EN.Ch07.Controls.CustomMetaTags::CreateChildControls")) { } }
if (SPContext.Current != null && SPContext.Current.File != null&& SPContext.Current.File.Item != null)
var item = SPContext.Current.File.Item;
if (item.Fields.ContainsField(FieldNameKeywords) && item[FieldNameKeywords] != null&& !string.IsNullOrEmpty(item[FieldNameKeywords].ToString()))
<meta>
element to the page using the format for the keywords tag and the value of the list item field:this.Controls.Add(new LiteralControl(string.Format(CultureInfo.InvariantCulture, FormatMetaTagKeywords, item[FieldNameKeywords].ToString())));
if (item.Fields.ContainsField(FieldNameDescription)&& item[FieldNameDescription] != null&& !string.IsNullOrEmpty(item[FieldNameDescription].ToString()))
<meta>
element to the page using the format for the description tag and the value of the list item field:this.Controls.Add(new LiteralControl(string.Format(CultureInfo.InvariantCulture, FormatMetaTagDescription, item[FieldNameDescription].ToString())));
Elements.xml
file of the new element, register our custom control with the AdditionalPageHead
delegate control using the following code:<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Control Id="AdditionalPageHead" Sequence="12" ControlClass="Code6587EN.Ch07.Controls.CustomMetaTags" ControlAssembly="$SharePoint.Project.AssemblyFullName$"></Control> </Elements>
CustomMetaTags
(the name of the class for our custom control)$SharePoint.Project.AssemblyFullName$
Code6587EN.Ch07.Controls
(the full namespace for the class, without the name of the class itself)True
True
CustomMetaTags
(the name of the class)<meta>
elements.In this recipe, if the current SharePoint content has a file associated with it, such as a web page, our control is looking at the list item for the file. If the list item has a Meta Keywords field or a Meta Description field, we are adding <meta>
tags to the page with the content of the fields.
3.15.220.16