This post is how to build composite page titles using an enhanced FieldValue WebControl.
Page Titles are very important for internet sites since more and more search engines rank pages higher that contain (parts of) the search phrase in the page title. That's why it makes sense to show keywords of your site in your page title - together with the actual location and maybe even the company name. This is what I would call a "composite page title".
Example? See http://www.schleuniger.com/DesktopDefault.aspx/tabid-44/74_read-1590/ from site which I've built some years ago (yes, I know it was not built using MOSS, but the business case is the same).
<title>PowerStrip 9500 BF Automatic wire cutting and stripping machine, cut, strip, wire stripping, coaxial cable stripping, automatic</title>
The title is built using the following pattern: <current location> <product related keywords (same for all products). If you check the other pages you'll find out that not all pages have these composite page titles - it is up to the editor to add them or not, but the <current location> part is of course always present.
I was thinking about how to realize this with MOSS. It is basically a very easy scenario, just add some "FieldValue" web controls in your page layout and that's it. However, I want things to be more "scalable". I want to be able to build composite page titles that do have separators, thus a "suffiix" or a "prefix". I even want to be able to provide a default if one part of the page title pattern does not contain a value (to add some default keywords or whatever). Now this is a little (tiny) bit more complicated.
Creating the control
I've created a new WebControl that inherits from BaseFieldControl:
using System;
using Microsoft.SharePoint.WebControls;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint.Security;
using System.ComponentModel;
namespace MichaelHofer.SharePoint.PublishingEnhancements
{
[SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true),
AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true),
AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class EnhancedFieldValue : BaseFieldControl
{
public EnhancedFieldValue()
{
base.DisableInputFieldLabel = true;
base.ControlMode = SPControlMode.Display;
}
Now I've added the properties I want to be able to configure when I use the control in my page layout or master page:
protected string m_Prefix = default(string);
[Bindable(true), Category("Data"), DefaultValue(""), Description("Prefix applied before the field's value.")]
public string Prefix
{
get { return m_Prefix; }
set { m_Prefix = value; }
}
protected string m_Suffix = default(string);
[Bindable(true), Category("Data"), DefaultValue(""), Description("Suffix applied after field's value.")]
public string Suffix
{
get { return m_Suffix; }
set { m_Suffix = value; }
}
protected string m_Default = default(string);
//[Bindable(true), Category("Data"), DefaultValue(""), Description("Default applied if field value is empty.")]
public string Default
{
get { return m_Default; }
set { m_Default = value; }
}
At last, I implement my own RednerFieldForDisplay-method:
[SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
protected override void RenderFieldForDisplay(HtmlTextWriter output)
{
string fieldValue = base.Field.GetFieldValueAsHtml(this.ItemFieldValue);
if (string.IsNullOrEmpty(fieldValue) && !string.IsNullOrEmpty(m_Default))
fieldValue = m_Default;
if (!string.IsNullOrEmpty(fieldValue))
output.Write(string.Format("{0}{1}{2}", m_Prefix, fieldValue, m_Suffix));
}
If there is no value given for the Field, the default will be applied. If even then there is nothing to write, prefix and suffix are not rendered, otherwise, if present, the value is rendered with the corresponding pre- and/or suffix.
Deyploment:
I won't go into the details of deploment in this post, everything is described in detail in my former post Part 2 (see below)
Creating composite page titles
In your page layout/master page editor (in my case SharePoint Designer), make sure that you have a reference to the assembly containing the EnhancedFieldValue control (see also my last post details). For some reason, SPD doesn't list the control in the "Web Controls (Sharepoint)" section. However, when you start typing the Prefix of the Assembly, it will show you the present controls. Now go ahead and create your composite page title. Example:
<%@ Register Tagprefix="PublishingEnhancements" Namespace="MichaelHofer.SharePoint.PublishingEnhancements" Assembly="MichaelHofer.SharePoint.PublishingEnhancements, Version=1.1.0.0, Culture=neutral, PublicKeyToken=7ce575c89ea427a4" %>
<asp:Content ContentPlaceholderID="PlaceHolderPageTitle" runat="server">
<SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
<PublishingEnhancements:EnhancedFieldValue runat="server" id="pageTitleExtensions" FieldName="Title_x0020_Extension" Prefix=" - " Suffix=" - Copyright 2007, Michael Hofer" Default="MOSS, WSS, SharePoint, Office" />
</asp:Content>
I'm using a custom field "Title Extension" which an editor can fill within the "Edit Mode Panel" for each field. If the field is blank or for some reason not present, the default is rendered, together with the specified pre- and suffix. The page title is then (no value given) rendered as:
Enhanced Page Titles - MOSS, WSS, SharePoint, Office - Copyright 2007, Michael Hofer
I think that justifies the small development that can be easily deployed using a clean and managable SharePoint Solution!
The next thing I will look at a little bit closer is how to easily (and easy to maintain!) landing pages for a MOSS site. What I mean is:
www.myinternetsite.com/products will redirect to www.myinternetsite.com/products/pages/default.aspx
www.myinternetsite.com/newsletter will redirect to www.myinternetsite.com/signup/default.aspx etc.
Please read also Part 1 & Part 2 of this series who are about building dynamic MetaTags.