Quantcast
Channel: Tech Blog
Viewing all articles
Browse latest Browse all 49

Display content from Reusable HTML list in a MasterPage

$
0
0

This is as useful as it is simple!

Having a footer for example in your master page without the overhead of being responsible for its maintenance.

Here is a simple WebControl that you can reference in your master page and supply a key.

Control

Note: You might want to handle the exception better!!

public class DisplayReusableContent : WebControl
{
    public string ContentTitle { get; set; }

    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        try
        {
            SPList rcl = SPContext.Current.Site.RootWeb.Lists["Reusable Content"];
            SPQuery q = new SPQuery
            {
                Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + ContentTitle + "</Value></Eq></Where>"
            };
            var items = rcl.GetItems(q);
            if (items != null && items.Count > 0)
            {
                // Should only be 1!
                writer.Write(items[0]["ReusableHtml"].ToString());
            }
        }
        catch (Exception ex)
        {
            writer.Write(ex.Message);
        }
    }
}

 

Register Control

<%@ Register TagPrefix="my" Namespace="$Your-Namespace" Assembly="$Assembl" %>

Use the Control

<my:DisplayReusableContent ContentTitle="Copyright" runat="server" id="ruc1" />

And that’s it – not life changing, but one for the tool-bag!

 


Viewing all articles
Browse latest Browse all 49

Trending Articles