Extension example: adding a new column to a list view
In this example, you modify the data returned by a GetList call and a GetListXml call to the Core Service by adding Schema information. With the Event Handler implemented, you can then add a column to a list view to show the new Schema information.
Procedure
- First, create your Extension in the form of a .NET class that extends the
TcmExtensionclass. This class includes the following:using System.Linq; using Tridion.ContentManager; using Tridion.ContentManager.ContentManagement; using Tridion.ContentManager.Extensibility; using Tridion.ContentManager.Extensibility.Events; namespace ListExtender { [TcmExtension("MyListEventHandlerExtension")] public class ListExtension : TcmExtension - In order to modify the data returned by the
GetListmethod, we intercept its response, which is a list of Repository Local Objects. To do so, we subscribe to the event of a specific type of Organizational Item (a Folder, say, or a Bundle) fetching a list of its contents. Specifically, we specify to check this in theProcessedEvent Phase, a phase in which the list will be available. The code for this subscription is as follows:public ListExtension() { Subscribe(); } public void Subscribe() { EventSystem.Subscribe<OrganizationalItem, GetListEventArgs<OrganizationalItemItemsFilter>>(CUSTOMMETHOD, EventPhases.Processed); }where CUSTOMMETHOD is your custom method that processes the list and adds a new property. For the sake of this example, we create a custom method that we call
AddMetadataSchemaTitle, which adds the name of the Metadata Schema on which an item is based as a new custom property. - Implement the custom method. For example, our
AddMetadataSchemaTitlemethod implements the following functionality:- It iterates through the items in the list.
- For each item, it checks if the item has a Metadata Schema defined. Note that for most items, this check means fully loading the item.
- If the item has a Metadata Schema, the title of that Metadata Schema is stored in an extension property called
SchemaTitle.
The extension properties will be added to the existing list results and sent along with them back to the client (both when the client is requesting data objects and when it is requesting an XML list). Here is the
AddMetadataSchemaTitlemethod:private void AddMetadataSchemaTitle(OrganizationalItem subject, GetListEventArgs<OrganizationalItemItemsFilter> e, EventPhases phase) { foreach (RepositoryLocalObject item in e.ListItems.OfType<RepositoryLocalObject>()) { if (item.MetadataSchema != null) { item.ExtensionProperties.Add("SchemaTitle", item.MetadataSchema.Title); } } } - This concludes the implementation of your Event Handler class. Save your class to a .cs file (ListExtension.cs in this example) and add it to your Visual Studio solution. Compile to create a new .NET assembly (ListExtender.dll in this example).
- If this Event Handler constitutes an Add-on feature to your product all by itself, or if this Event Handler is part of a larger Add-on feature for which you have not yet created an Add-on ZIP file, create a new ZIP file for your Add-on feature.
- Add your Event Handler .NET assembly file to the Add-on feature ZIP file to which it belongs. Place it in a new root-level folder in the ZIP file that you call ListExtender\.
- Decide in which SDL Tridion Sites user interface or user interfaces you want the extended list to appear (Content Manager Explorer, Experience Manager, your own custom UI), and in which views of the user interface you want the list to be extended. In this example, we add the extension to Content Manager Explorer only, and only to its item select dialogs (so not to the ordinary list view on the main screen).
- Create an initial configuration file for your Editor as explained elsewhere in the documentation and call it ListExtenderEditor.config.
- In the
extensionssection, locate theext:editorextensionssection. Inside this section, create or find anext:editorextensionelement with itstargetattribute set to the identifier of the user interface you want to extend. For example, to extend Content Manager Explorer, find<ext:editorextension target="CME">. - Within this
ext:editorextensionelement, ensure the presence of anext:listschild subelement, and within thatext:listselement, add the extension configuration in the form of anext:addchild element, as follows:<ext:add> <ext:extension name="EXTENSIONNAME" assignid="EXTENSIONID"> <ext:listDefinition> <ext:selectornamespaces> <list:namespace prefix="ext" uri="http://www.tridion.com/ContentManager/R5/Extension" /> </ext:selectornamespaces> <ext:columns> <list:column id="COLUMNID type="data" title="COLUMNHEADERNAME" selector="XPATH" /> </ext:columns> </ext:listDefinition> <ext:apply> <ext:view name="VIEWNAME"> <ext:control id="FilteredItemList" /> </ext:view> </ext:apply> </ext:extension> </ext:add>where:- EXTENSIONNAME is a name for your extension, for example,
MyColumnExtender - EXTENSIONID is a unique identifier for your extension, for example,
MyColumnExtenderID - COLUMNID is a unique identifier for the new column in your view, for example,
MyColumnID - COLUMNHEADERNAME is the label that will appear in the user interface as the column header; in our example,
Schemawould be a good value - XPATH is the XPath that gets the value to put in this column; in our example, this would be
@ext:SchemaTitle, referring to the newSchemaTitleproperty we have added. - VIEWNAME is the name of the view in which the list should show this additional column; in our example, this would be
ItemSelectDialogPopup.
- EXTENSIONNAME is a name for your extension, for example,
- If you wish, specify one or more of the following additional attributes in the
list:columnelement:Attribute name Value description widthThe default width in pixels of the column when the view loads initially. For example, to set initial width to 60 pixels, set this attribute's value to 60px.enableresizingEnables users to change the width of this column, if set to true.enablesortingEnables users to sort the data in this column, if set to true.enablefilteringEnables users to filter the data this column, if set to true.columnfilterSet to one of the following values:value- The user can filter the column on specific values. Use this value if you expect your column to contain only a small number of different values.
group- The user can filter the column on groups of values, as automatically generated by SDL Tridion Sites. Use this value if you expect your column to contain a large number of different values.
daterange- If your column contains dates, this filter enables users to filter on a specific date range.
- Save and close ListExtenderEditor.config.
- Add your Editor folder tree to the ListExtender\ folder in the same Add-on package ZIP file to which you already added ListExtender.dll.
- Extract the Add-on feature manifest file, manifest.json, from the Add-on feature ZIP file, and open it for editing.
- In the
extensionsarray, insert the following JSON fragment:{ "name": "ListExtender", "type": "CMEventHandler", "properties": { "assemblyFileSource": "ListExtender\\ListExtender.dll" } }, { "name":"ListExtenderEditor", "type": "UIEditor", "properties": { "name": "ListExtenderEditor", "contentFolder": "ListExtender\\Editor", "configurationPath": "ListExtender\\Editor\\Configuration\\ListExtenderEditor.config", "assembliesFolder": "ListExtender\\Editor\\Assemblies", "globalResourcesFolder": "ListExtender\\Editor\\Resources" } } - Save and close the modified manifest.json file and update it in the Add-on ZIP file.
- Deploy or redeploy the Add-on feature ZIP file from the Add-ons screen in Content Manager Explorer.
Your extension now shows up in the user interfaces and views you specified.