Data Extender example: adding a new column to a list view

In this example of a Data Extender, you modify the data returned by a GetList() call by adding Schema information. With the Data Extender implemented, you can then add a column to a list view to show the new Schema information.

Procedure

  1. First, create your Data Extender in the form of a .NET class that extends the DataExtender class. This class includes the following:
    using System;
    using System.Xml;
    using System.IO;
    
    using Tridion.Web.UI.Core;
    using Tridion.Web.UI.Core.Extensibility;
    using Tridion.ContentManager;
    using MyCustomModel.CoreService;
  2. We must implement the ProcessRequest method, which handles requests, but because we do not need to do anything with requests, we make it a dummy method:
    public override XmlTextReader ProcessRequest(XmlTextReader reader, PipelineContext context)
    {
      return reader;
    }
  3. Rather, in this example situation, we want to intercept the response of any GetList or GetListCheckedOutItems command. We do this by implementing the ProcessResponse method:
    public override XmlTextReader ProcessResponse(XmlTextReader reader, PipelineContext context)
    {
    		XmlTextReader xReader = reader;
    		string command = context.Parameters["command"] as String;
    		if (command == "GetList" || command == "GetListCheckedOutItems")
    		{
    				try
    		 	{
    					 xReader = AddSchemaTitle(reader, context);
    				}
    				catch
    				{
          // Handle exception 
        }
    		}
    		return xReader;
    }

    where AddSchemaTitle is the method that actually processes the XML returned.

  4. Implement AddSchemaTitle. This method mostly just copies the response XML. Here is the relevant part of the method, where it actually adds a Schema title to the XML:
    // Get the unique identifier of the current item.
    string id = xReader.GetAttribute("ID");
    try
    {
    	 // Get the Content Manager URI of the current item, using its identifier.
    	 TcmUri uri = new TcmUri(id);
    	 if (uri.ItemType != ItemType.None)
      {
        // Connect to the Core Service.
        if (client == null)
        {
          client = new SessionAwareCoreService2010Client("TcmBinaryEndpoint");
          client.Impersonate(Utils.GetUserName());
        }
        IdentifiableObjectData io = client.Read(id, null);
        LinkToSchemaData schemaLink = null;
    
      		// Find this item's Schema, if it has one, based on the item's item type. 
    		  // For Components, this means its Schema; for all other items, their Metadata Schema. 
        if (uri.ItemType == ItemType.Publication)
        {
          RepositoryData repository = io as RepositoryData;
          schemaLink = repository.MetadataSchema;
        }
        else if (uri.ItemType == ItemType.Component)
        {
          ComponentData component = io as ComponentData;
          if (component != null)
          {
            schemaLink = component.Schema;
          }
        }
        else
        {
          RepositoryLocalObjectData rlo = io as RepositoryLocalObjectData;
          if (rlo != null)
          {
            schemaLink = rlo.MetadataSchema;
          }
        }
    
      		// If this item has a (Metadata) Schema, get its title 
    		  // and place it in a new attribute called SchemaTitle.
        if (schemaLink != null)
        {
          xWriter.WriteAttributeString("SchemaTitle", schemaLink.Title);
        }
      }
    }
    catch (Exception)
    {
      if (client != null)
      {
        client.Abort();
        client = null;
      }
    }
    You now have a class that adds a new SchemaTitle attribute to some of the returned items, set to the name of the item's associated Schema or Metadata Schema.
  5. Save and close your Data Extender class to a .cs file and add it to your Visual Studio solution.
  6. Add this Data Extender to the configuration file of your custom Editor (or of your custom Data Model, if it has its own configuration file) by opening that file and navigating to the <ext:dataextenders> section. Add the Data Extender by adding a new ext:dataextender element in this section:
    <ext:dataextenders>
      <ext:dataextender type="MyExtenderClassName, MyAssemblyName" name="My Data Extender">
        <ext:description>Schema Title Data Extender</ext:description>
      </ext:dataextender>
    </ext:dataextenders>

    where MyExtenderClassName, MyAssemblyName is the assembly qualified name.

  7. Now define the extra column in the Content Manager Explorer. In the extensions section, locate the ext:lists section. Inside this section, create or find an <ext:add> element.
  8. Insert an ext:extension element that looks as follows:
    <ext:extension name="MyColumnExtender" assignid="MyColumnExtenderID">
      <ext:listDefinition>
        <ext:selectornamespaces />
        <ext:columns>
          <ext:column xmlns="http://www.sdltridion.com/2009/GUI/Extensions/List"
                      id="MyColumnID" type="data" title="Schema"
                      selector="@SchemaTitle" translate="String" />
        </ext:columns>
      </ext:listDefinition>
      <ext:apply>
        <ext:view name="DashboardView" />
      </ext:apply>
    </ext:extension>

    This code introduces a new column with a unique idenfitier called MyColumnID and fills it the data indicated by the XPath expression in the selector attribute. In this example, the XPath expression retrieves the SchemaTitle attribute, which our Data Extender adds to the items being retrieved. The display label of the column header will be the value of the title attribute.

  9. You can use the following (optional) attributes of ext:column to control the appearance of the new column:
    width
    Set to the desired (default) width of the column, e.g. "60px" for 60 pixels.
    enableresizing
    Set to true to enable users to resize this column.
    enablesorting
    Set to true to enable users to sort the data in this column.
    enablefiltering
    Set to true to enable users to filter the data in this column.
    columnfilter
    Set to value to enable users to filter on specific values for this column; to group to enable filtering on groups of values (as created automatically by the GUI); or to daterange if your column contains dates and you want to enable users to filter on a specific date range.
  10. If your selector attribute contains an XPath that uses XML namespace prefixes, then declare those prefixes in the ext:selectornamespaces element. The following example defines two namespace prefixes, tcm and xlink:
    <ext:selectornamespaces>
      <list:selectornamespaces>
        <list:namespace prefix="tcm" uri="http://www.tridion.com/ContentManager/5.0"/>
        <list:namespace prefix="xlink" uri="http://www.w3.org/1999/xlink"/>
      </list:selectornamespaces>
    </ext:selectornamespaces>