Although the Reusable Content list works well for adding content to content placeholders in a publishing page, there are times when I want to use it to store reusable content that should show up in my master page or in page layouts. For instance, I’d like the copyright message that appears in the footer to be maintained by content owners, so that each time a new year rolls by, they can update the date in the footer without needing to get a developer involved to update the master page.

This is a control I’ve developed that retrieves a particular item from the Reusable Content list by the item’s title, and displays it. It retrieves the item by a simple CAML query.

using System;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace BB.SP2010.WebControls
{
  public class ReusableContentControl : WebControl
  {
    private string reusableContentListItemTitle;
    public string ReusableContentListItemTitle
    {
      set { reusableContentListItemTitle = value; }
    }

    protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
    {
      SPWeb rootWeb = SPContext.Current.Site.RootWeb;
      SPList reusableContentList = rootWeb.Lists["Reusable Content"];
      SPQuery query = new SPQuery();
      query.Query = String.Format("{0}",
      reusableContentListItemTitle);
      SPListItemCollection listItems = reusableContentList.GetItems(query);
      if (listItems.Count > 0)
      {
        SPListItem listItem = listItems[0];
        Literal reusableContent = new Literal();
        reusableContent.Text = listItem["Reusable HTML"].ToString();
        this.Controls.Add(reusableContent);
      }
      base.RenderChildren(writer);
    }
  }
}

To use this control, first add a reference to the assmebly in your page, like this:

<%@ Register Tagprefix="CustomWebControls" Namespace="BB.SP2010.WebControls" Assembly="BB.SP2010.WebControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2f91cfdda5cd365b" %>

Next, add the control to your page, passing in the title of the Reusable List list item you want to render to the page:

<CustomWebControls:ReusableContentControl runat="server" ID="copyrightControl" ReusableContentListItemTitle="Copyright"/>

I’m not caching the results because, if you have caching turned on on your Publishing site, the CAML query itself should be cached.