1

[Tridion beginner]

I have a folder named Components where I store different Components. Now, I would like (in C# code fragment template building block that I put in component template) to get list of components from this folder, based on certain schema, and put them in package, so I can later render them from DWT template building block.

This is what I have:

<%@Import Namespace="Tridion.ContentManager.ContentManagement"%>


  // run template with article component, so that article schema is processed

  string componentID = package.GetValue("Component.ID");
  Component articleComponent = (Component)engine.GetSession().GetObject(componentID);

  // Get the Schema of articleComponent
  Schema articleSchema = articleComponent.Schema;

  // get the folder of current component
  Folder componentsFolder = (Folder) articleComponent.OrganizationalItem;

  // Create a object of class Filter to filter retrieved content items.
  Filter filter = new Filter();

  // Filter out all items that are not based on the Schema called articleSchema.
  filter.Conditions["BasedOnSchema"] = articleSchema.Id;

  // Make the call to GetListItems() using this Filter object. For each item, the default set of items is returned.
  XmlElement listOfComponents = componentsFolder.GetListItems(filter );

  package.PushItem("listOfComponents", package.CreateStringItem(ContentType.Text,  listOfComponents.ToString()));    

I have pushed XmlElement as String in package so I could see what am I getting, but all I get is just one line - "XmlElement". What am I doing wrong with reading components from folder, because there are couple components with appropriate schema in this folder.

0

You are inserting the definition of XmlElement instead of the values contained in it. Probably you can convert the XmlElement in to XmlDocment and retrieve the results . Below is the syntax

        XmlDocument testDocument = new XmlDocument();
        testDocument = listOfComponents.OwnerDocument;
        package.PushItem("Output", package.CreateXmlDocumentItem(ContentType.Xml, testDocument ));

If you need the output to be in string then use below

        package.CreateStringItem(ContentType.Text, listOfComponents.InnerXml.ToString())

Also note the method GetListItems(filter) is depreciated.

  • Thanks for help. Another possible solution could be using GetItems() method: List<TcmUri> listOfComponents = new List<TcmUri>(); foreach (Component c in componentsFolder.GetItems(filter)) { listOfComponents.Add(c.Id); } – Vladimir Jul 15 '14 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.