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
- First, create your Data Extender in the form of a .NET class that extends the
DataExtenderclass. 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; - We must implement the
ProcessRequestmethod, 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; } - Rather, in this example situation, we want to intercept the response of any
GetListorGetListCheckedOutItemscommand. We do this by implementing theProcessResponsemethod: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
AddSchemaTitleis the method that actually processes the XML returned. - 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 newSchemaTitleattribute to some of the returned items, set to the name of the item's associated Schema or Metadata Schema. - Save and close your Data Extender class to a .cs file and add it to your Visual Studio solution.
- Add this Data Extender to the configuration file of your custom Editor (or of your custom 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 newext:dataextenderelement in this section:<ext:dataextenders> <ext:dataextender type="MyExtenderClassName, MyAssemblyName" name="My Data Extender" critical="true"> <ext:description>Schema Title Data Extender</ext:description> </ext:dataextender> </ext:dataextenders>where MyExtenderClassName, MyAssemblyName is the assembly qualified name.
The
criticalattribute, set totrue, causes a redirect to an error page if creation of the Data Extender fails. You can omit this attribute, or explicitly set it tofalse, if you do not consider the Data Extender business-critical. SDL Tridion will then log an error but continue anyway. - Now define the extra column in the Content Manager Explorer. In the
extensionssection, locate theext:listssection. Inside this section, create or find an<ext:add>element. - Insert an
ext:extensionelement 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
selectorattribute. 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 thetitleattribute.Note: If you build your own extender and the data returned is not textual but an icon or a checkbox, set the value of thetypeattribute toiconor tocheckbox, respectively. - You can use the following (optional) attributes of
ext:columnto 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
trueto enable users to resize this column. enablesorting-
Set to
trueto enable users to sort the data in this column. enablefiltering-
Set to
trueto enable users to filter the data in this column. columnfilter-
Set to
valueto enable users to filter on specific values for this column; togroupto enable filtering on groups of values (as created automatically by the GUI); or todaterangeif your column contains dates and you want to enable users to filter on a specific date range.
- If your
selectorattribute contains an XPath that uses XML namespace prefixes, then declare those prefixes in theext:selectornamespaceselement. The following example defines two namespace prefixes,tcmandxlink:<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>