Adding meta tags to pages from custom library fields with an AdditionalPageHead delegate control

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.

Getting ready

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.

How to do it...

Follow these steps to add the META tags with a delegate control:

  1. Open the project created in the Creating a Visual Studio solution for custom delegate controls recipe of this chapter in Visual Studio.
  2. Add a new class to our Controls folder.
  3. Set the access modifier of our new class to public and inherit from the UserControl base class:
    public class CustomMetaTags : UserControl
  4. Set constant values for the names of the fields to look for and the formats of the <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}" />";
  5. Override the CreateChildControls method and add a monitored scope:
    protected override void CreateChildControls()
    {
      using (new SPMonitoredScope("Code6587EN.Ch07.Controls.CustomMetaTags::CreateChildControls"))
      {
      }
    }
  6. Ensure the current SharePoint context is not null, it has a file, and that the file has a list item:
    if (SPContext.Current != null && SPContext.Current.File != null&& SPContext.Current.File.Item != null)
  7. Get the list item associated with the current file as follows:
    var item = SPContext.Current.File.Item;
  8. Ensure the list has the Meta Keywords column and that the current item has a value assigned:
    if (item.Fields.ContainsField(FieldNameKeywords) && item[FieldNameKeywords] != null&& !string.IsNullOrEmpty(item[FieldNameKeywords].ToString()))
  9. Add a <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())));
  10. Ensure the list has a Meta Description column and that the current item has a value assigned:
    if (item.Fields.ContainsField(FieldNameDescription)&& item[FieldNameDescription] != null&& !string.IsNullOrEmpty(item[FieldNameDescription].ToString()))
  11. Add a <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())));
  12. Add a new Empty Element item to the project.
  13. In the 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>
  14. Add a new safe control entry to the new element with the following details:
    • (Name): CustomMetaTags (the name of the class for our custom control)
    • Assembly: $SharePoint.Project.AssemblyFullName$
    • Namespace: Code6587EN.Ch07.Controls (the full namespace for the class, without the name of the class itself)
    • Safe: True
    • Safe Against Script: True
    • Type Name: CustomMetaTags (the name of the class)
  15. Open the feature created previously. Ensure that the new element is included in the feature.
  16. Click on Start from the toolbar to deploy the solution and attach the debugger.
  17. Once the SharePoint site is loaded in the web browser (after clicking on Start), navigate to a page with either the Meta Keywords field or the Meta Description field populated. View the source of the page to observe the addition of the <meta> elements.

How it works...

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.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.220.16