As well as the ROBOTS META tag, you can emit other
META tags to help indexing by the particular search engine you are
working with. Common tags used by search engines include the <title> tag and the Description and Keywords META tags.
In MCMS, channels and postings have Description and DisplayName properties, which are often suitable for the Description META tag and the <title> tag respectively.
Custom properties can also be used as META tags and rendered as <meta name="CustomPropertyName" content="CustomPropertyValue">
Keywords can be entered
by authors and stored as custom property values. What you will need to
do then is to retrieve these values from the repository and display them
as META tags in the head region of the page.
There are several ways you can go about this:
The fastest method is to simply use <%= %> constructs or Response.Write() calls within the template HTML code.
If content that resides in custom properties or placeholders, you can retrieve it using code-behind files.
Like regular controls,
use a web user control to share code among template files in the same
project. If you need to generate the same set of tags across pages
stored in multiple projects, consider writing a custom control as we did
for the ROBOTS tag earlier.
Using <%= %> within Template Files
Use <%= %> constructs or the Response.Write()
method when you need to display single pieces of information, like
display names and descriptions. Let’s take a closer look by implementing
the constructs in the Columns.aspx template file of the Tropical Green project.
1. | Open the Tropical Green project. Open the Columns.aspx template file in HTML view.
|
2. | At the top of the page, add a page directive to import the Microsoft.ContentManagement.Publishing namespace. This allows us to access its classes (such as CmsHttpContext) from within the template file. <%@ Import Namespace="Microsoft.ContentManagement.Publishing" %>
|
3. | Look for the <title></title> tags between the <Head> tags. Replace them with: <title><%= CmsHttpContext.Current.Posting.DisplayName %></title>
|
4. | Below the <title> tag, add a tag to display the description of the posting:
<meta name="Description" content="<%=
CmsHttpContext.Current.Posting.Description %>">
|
5. | Save Columns.aspx.
|
When you next view a posting using the Columns template, you will see the DisplayName of the page in the browser’s title bar.
To view the META tags, right-click on the page and select View Source. The Description META tag is nested between the <head>
tags. You don’t get to see it displayed in the browser, as it is used
solely for search engines as they index your site. The source should
look like:
<title>A Walk Through Jenny's Garden</title>
<meta name="Description" content="You can publish pages directly from Word to
MCMS using Authoring Connector!">
Pulling META Tag Content from Custom Properties
Some META tags are
more complex and can’t be generated by simply retrieving property values
of postings. Say you have added a custom property called Keywords
to every template object created. Authors enter keywords to describe
the posting when they create it, which are then used for the custom
property.
In this section,
we look at two alternatives for accessing that information. The first
option we will explore is adding literal controls to the template files.
In the second option we will create a user control to output the META
tags.
Using Literal Controls
To pull content from custom properties, you could add a literal control between the <head> tags as shown below:
<Head>
<asp:Literal id="litKeywords" runat="server"></asp:Literal>
. . . code continues . . .
</Head>
Then, in the Page_Load() event handler of the code-behind file, you could retrieve the value of the custom property and insert it as the attribute of a <meta name="Keywords"> tag:
using Microsoft.ContentManagement.Publishing;
... code continues ...
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Posting currentPosting = CmsHttpContext.Current.Posting;
string keywords = "";
CustomProperty cpKeywords = currentPosting.CustomProperties["Keywords"];
if(cpKeywords!=null)
{
keywords = cpKeywords.Value;
}
litKeywords.Text = "<meta name=\"Keywords\" content=\"" + keywords + "\">";
}
Overriding the Render Method of a Web User Control
Here’s another approach: instead of using Literal controls within the <head> tags, you could create a web user control and override its Render() method. First get an instance of the posting, then get its custom property value, and finally create the META tag:
namespace TropicalGreen.UserControls
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
// MCMS PAPI
using Microsoft.ContentManagement.Publishing;
// Namespace for HtmlTextWriter
using System.Web.UI;
public abstract class MetaTags : System.Web.UI.UserControl
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
protected override void Render( HtmlTextWriter writer )
{
CmsHttpContext cmsContext = CmsHttpContext.Current;
Posting p = cmsContext.Posting;
string keywords = "";
CustomProperty cpKeywords = p.CustomProperties["Keywords"];
if (cpKeywords != null)
{
keywords = cpKeywords.Value;
}
writer.WriteLine("<meta name=\"keywords\" Content=\""
+ keywords +"\">");
}
#region Web Form Designer generated code
. . . code continues . . .
#endregion
}
}
The MetaTags user control we have just created could also be enhanced to output the posting description and custom properties as META tags.
You can use the
same technique to add placeholder content to META tags. The MCMS
Connector for SharePoint Technologies contains the customizable SearchMetaTagGenerator server control to generate META tags from properties and custom properties.