EnhancedList Visualforce Component

The enhancedList Visualforce component is an awesome little tag that allows you to display a list view on a Visualforce page. One of the cool features is that you create a custom list view, e.g., "Super Special Contacts", and just show those records in the list view by specifying the ID of the list view listId attribute. That way you can setup a tab that just displays this specific list view and not allow the user to select other list views from the picklist (it doesn't display).

The only problem with this approach is that the id for the custom list view that you enter in the listId will most likely change during deployments or in a managed pacakge.

So I created the following Visualforce componenet that you can throw in your Visualforce page and use the name of the custom list view instead of the id. All of the code is available in this gist. Enjoy!


This is the component that provides the enhanced list view functionality. It uses the Apex Controller below.

<apex:component controller="ContactListViewController">  
  <apex:attribute name="listViewName" type="String" required="true" 
    description="The name of the listview." assignTo="{!listName}"/>    

  <apex:enhancedList height="400" rowsPerPage="25" id="ContactList"
    listId="{!listId}" rendered="{!listId != null}" />

  <apex:outputText rendered="{!listId == null}" value="Could not find requewed ListView: '{!listName}'. Please contact your administrator."/>



The Apex Controller for the component simply looks up the Id of the list view by name and returns the Id.

public with sharing class ContactListViewController {

  public String listName {
    set {
        listName = value;
        String qry = 'SELECT Name FROM Contact LIMIT 1';
        ApexPages.StandardSetController ssc = 
            new ApexPages.StandardSetController(Database.getQueryLocator(qry));
        List<SelectOption> allViews = ssc.getListViewOptions();
        for (SelectOption so : allViews) {
          if (so.getLabel() == listName) {
            // for some reason, won't work with 18 digit ID
            listId = so.getValue().substring(0,15);
  public String listId {get;set;}



Here's a sample Visualforce page using the component.

  <c:ContactListViewComponent listViewName="Super Special Contacts"/>


And finally, no Salesforce code would be complete without test cases so ...

private class TEST_ContactListViewController {  
  private testMethod static void testSuccess() {
    ContactListViewController con = new ContactListViewController();
    con.listName = 'Super Special Contacts';
    System.assert(con.listId != null);

  private testMethod static void testFailure() {
    ContactListViewController con = new ContactListViewController();
    con.listName = 'BADLISTNAME';
    System.assert(con.listId == null);