Wednesday 4 March 2009

Adding Metadata to a MOSS 2007 Publishing Portal pt2

Add three columns to the Content Type that you use for WCM. In this example I've used PageTitle, PageDescription and PageKeywords.

Add the assembly to the Master Page and reference the MetaData dll
<%@ Register TagPrefix="metaControl" Namespace="MetaData.Render" Assembly="MetaData.Render, Version=1.0.0.0, Culture=neutral, PublicKeyToken=198d2f392e054e3e" %>

Example: In the head
<metaControl:SharePointMetaTag runat="server" ID="metaTag1" MetaName="DESCRIPTION" MetaAttributeName="Content" PublishingPagePropertyName="PageDescription" DefaultAttributeContent=""/>


The DefaultAttributeContent is where the editor has not completed the field as we've made them non manditory

namespace MetaData.Render
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
// using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

///
/// A webpart to render MetaData information onto a published page
///

[ParseChildren(false), ToolboxData("<{0}:MetaTag runat=\"server\" name=\"\"> "), PersistChildren(true),
AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal),
DefaultProperty("Name")]
public class MetaData : WebControl
{
///
/// Name to render out
///

private string name;

///
/// Initializes a new instance of the MetaData class.
///

public MetaData() : base(HtmlTextWriterTag.Meta)
{
}

///
/// Gets or sets the Tag name to render out
///

[Bindable(true), Category("Appearance"), DefaultValue(""), Description("Sets the MetaTag's name attribute.")]
public string Name
{
get { return this.name; }
set { this.name = value; }
}

///
/// Render the control
///

/// Render this writer
protected override void Render(HtmlTextWriter writer)
{
// Render control only if there is a tagname specified
if (!this.name.Equals(string.Empty))
{
base.Render(writer);
}
}

///
/// Add attributes to Render
///

/// The writer to add the attributes to
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
// Write the name attribute
writer.AddAttribute("name", this.name);

// Write the value attribute
StringWriter sw = new StringWriter();
base.RenderContents(new HtmlTextWriter(sw));
writer.AddAttribute("value", sw.ToString());
}

///
/// Render the content out
///

/// The writer to be rendered
protected override void RenderContents(HtmlTextWriter writer)
{
// this control does not render any contents
}
}
}

No comments: