Tridion Generic .NET Collections

 

The classes provided in this eXtension give an easy and convenient way for loading xml lists of items that are generated from the GetListxxxx methods in the Tridion API. This eXtension provides two variation for both the .NET interops and the TOM.NET API.

While developing Tridion templates or any other code using the Tridion Content Management API a reoccuring  activity is of loading lists of items in XML and then parsing those to be able to retrieve the necessary items data, this usually results in very similar code being written over and over again which is both tedious and annoying.

The idea behind this eXtension is to both simplify and abstract this process so it can be reused across the different areas and places where this kind of xml list parsing is required.

Calling the GetListxxxx methods in the Tridion API is preferable to the GetItems methods which return a collection of objects and therefore are more expansive in terms of system resources, the generic collection described here will only load the object into memory when its explicitly required to. The caveat is that the collection is not designed to be iterated over more than once since this will cause it to reload each object into memory for every iteration which is an expansive operation.
If there is a need to loop through the collection items more than once its recommended to use the GetItems method of the collection to retrieve a collection of instantiated objects.

 

Interops (COM API) 

For this API which is still being heavily used for developing custom code or Event System implementations the class in the Tridion.PS.TridionCollections.interops namespace can be used.

Here's an example of using it:

ListRowFilter folderFilter = tdse.CreateListRowFilter();
folderFilter.SetCondition("ItemType", ItemType.ItemTypeFolder);
folderFilter.SetCondition("Recursive", false);

TridionCollection<Folder> folders = new TridionCollection<Folder>(tdse.GetListItems(ListColumnFilter.XMLListIDAndTitle, folderFilter));

foreach (Folder f in folders)
{
      Console.WriteLine("Title: " + f.Title);
}

In this example, we load a XML list of folders into the Tridion collection and simply iterate over it to display the titles of each folder.

 

TOM.NET

For this API which is currently available only when developing templates in Visual Studio the class in the Tridion.PS.Tridioncollections.dotNET can be used.

Here's an example of using it:

Filter m_Filter = new Filter();
m_Filter.Conditions.Add(new KeyValuePair<string, object>("recursive", false));
m_Filter.Conditions.Add(new KeyValuePair<string, object>("ItemType", 4)); 

StructureGroup rootSG = engine.GetObject("tcm:2-65-4"); 
XPathExpression listXpath = XPathExpression.Compile("/tcm:ListItems/tcm:Item[contains(./@Title,'abc')]"); 
TridionCollectionBase<StructureGroup> list = new TridionCollectionBase<StructureGroup>(m_Engine, rootSG, m_Filter, listXpath); 

In this example we create a collection of StructureGroup by passing in a root StructureGroup, a filter object and a specific XPath expression to filter out items matching the criteria.

 

Disclaimer 

The code provided here hasnt been thorougly tested and its stability or performance hasnt been confirmed fully. Its purpose is to provide a convenient and quick way of developing Tridion code but before applying this code to a production environment it should be tested extensively.

The code is very generic and can definitly be extended and modified, if you have a good idea about adding to it or making it better please let me know at: yniran@sdl.com


Developer
:    Yoav Niran
Company
License agreement
Tags
:    Templating,  Event System,  .NET,  TOM.NET,  Interops
Version
:   1.0
Source
:   Yes

About the Author
Yoav Niran
Senior Technical Consultant

Yoav joined Tridion in 2006 and is working in the Professional Services Maidenhead office as a senior technical consultant. 

He has been involved in numerous implementations, integrations and development of packaged services.

Yoav has much experience in .NET technologies and ASP.NET web development in particular.

He has also been the technical lead on the reconstruction of the SDL Tridion World website.

For more technical articles from Yoav go to: http://yoavniran.wordpress.com

SDL CMT division

SDL Tridion Certification!

The SDL Tridion Certification Program for Technical Consultants is a Professional Services initiative targeted at the SDL Tridion Technical Consultant community.