First of all, thanks to Andy Kinnear for writing this piece of code! What this control does is to allow you to add a list view to any layout or master page :)

Notes:

  • You only need to specify the name of the list (no GUID)
  • You are able to type in the name of the view you would like displayed; if you dont specify a view name it will display the default view of this list. 
  • The custom control will only look for the frist list name as specified it can find in the current site, or any sites above the current site if the list name is not in the current site.
  • You can only specify the list name and view name in master / layout page where you add the control.

INSTRUCTIONS:

  • Build the class:

    using System;
    using System.ComponentModel;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    using Microsoft.SharePoint.Publishing.Navigation;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint;
    using PortalCustomNav.ComponentModel;

    namespace PortalCustomNav.WebControls
    {
    [ToolboxData("<{0}:ListViewControl runat=\"server\" />")]
    public class ListViewControl : SPControl
    {

    [Browsable(true)]
    [SPCategory("SPCategory_Configuration")]
    [DefaultValue("")]
    [SPDescription("ListControlDescription_ListName")]

    private string _listName = "";

    public string ListName
    {
    get
    {
    return _listName;
    }
    set
    {
    _listName = value;
    }
    }

    [Browsable(true)]
    [SPCategory("SPCategory_Configuration")]
    [DefaultValue("")]
    [SPDescription("ListControlDescription_ListName")]

    private string _listViewName = "";

    public string ListViewName
    {
    get
    {
    return _listViewName;
    }
    set
    {
    _listViewName = value;
    }
    }

    protected SPList GetCurrentList(SPWeb site)
    {

    SPListCollection view = site.GetListsOfType(SPBaseType.GenericList);

    foreach (SPList list in view)
    {
    if (list.Title == this._listName)
    {
    return list;
    }
    }

    if (site.ParentWeb != null)
    {
    return GetCurrentList(site.ParentWeb);
    }
    return null;
    }

    protected SPList GetCurrentList()
    {
    SPWeb site = Microsoft.SharePoint.SPContext.Current.Web;
    SPList list = GetCurrentList(site);
    return list;
    }

    //Render the list view
    protected override void Render(HtmlTextWriter writer)
    {

    SPList lst = GetCurrentList();

    if (lst != null)
    {

    foreach (SPView view in lst.Views)
    {
    if (view.Title == this._listViewName)
    {
    SPQuery query = new SPQuery(view);
    writer.Write(lst.RenderAsHtml(query));
    return;
    }
    }

    writer.Write("View " + this._listViewName + " not found in list");

    }
    else
    {
    writer.Write ("List "+this._listName+" not found in site or parent site");
    }
    }

    }
    }
  • Register the class in the GAC and add a "Safe Control" entry in the web.conf file.
  • Register the control in the master page:

    <%@ Register Tagprefix="PortalCustomNav" Namespace="PortalCustomNav.WebControls" Assembly="PortalCustomNav, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0c07a529847e943d" %>

  • Add the control to the master page:

    <PortalCustomNav:ListViewControl runat="server" ListName="Comments" ListViewName=""></PortalCustomNav:ListViewControl>