-9

I have components under folders.

What code/codesyntax needs to be followed, to loop through a folder which have components so that i can pull the values from different components.

Could anyone help me out.

I have used the following code

**Folder compFilter= new Filter(); 
Folder folder=engine.GetObject(webdavurl) as Folder; 
foreach(Component comp in folder.GetItems(CompFilter))**

Its not working.Please suggest some solution.

  • 1
    Please specify which version of Tridion and if the code is in a script or a template, and if in a template, which template language. – robrtc Jul 12 '12 at 16:01
  • 1
    Writing C# Code. Version 2011 – user1518281 Jul 12 '12 at 16:04
  • 3
    the first line of your code sample contains a problem. The compFilter object is not of type Folder The C# example here http://www.tridiondeveloper.com/search-engine-sitemap-xml-generation-using-sdl-tridion shows how to loop through Structures and pages, simply convert for Folders and Components – johnwinter Jul 12 '12 at 16:10
  • 2
    "Its not working" is very vague, at minimum you should inform us what the result is, are you getting an empty collection or are you getting an error message etc. – Bart Koopman Jul 12 '12 at 16:39
  • @user1518281, doing some preliminary research on the web is expected prior to asking on SO. There are many examples available online and in the official Tridion documentation about this. – Nickoli Roussakov Jul 12 '12 at 16:40

2 Answers2

5

The logic is the following:

  1. Create a filter specifying that the item types you're interested in is Components
  2. Get Items or Get List Items from the folder, depending on the information you need. If all you need are titles and Ids, Get List is faster (returns XML). If you need additional attributes from the items, then use Get Items which returns objects.

As mentioned, if you let us know which API you may get a code sample. The above logic applies to any of the APIs.

[Update]

Please don't use Filter, this class was deprecated in Tridion 2011 in favor of the StronglyTypedFilter "family" instead.

Session session = engine.GetSession();
string folderUrl = "/my/long/webdav";
OrganizationalItemItemsFilter filter = new OrganizationalItemItemsFilter(session)
                                            {ItemTypes = new[] {ItemType.Component}};

Folder folder = (Folder)session.GetObject(folderUrl);

foreach (Component component in folder.GetItems(filter))
{
    // Do something with the component
}
Nuno Linhares
  • 10,214
  • 1
  • 22
  • 42
4

A simple example of how to use the 2011 TOM.NET API looping over a Folder and getting to the Components would be:

OrganizationalItemItemsFilter filter = new OrganizationalItemItemsFilter(folder.Session)
{
    ItemTypes = new[] {ItemType.Component},
    Recursive = false
};
foreach (Component comp in folder.GetItems(filter))
{
    // get fields collection of component
    ItemFields fields = new ItemFields(comp.Content, comp.Schema);
}

But if you haven't already gotten this far I strongly suggest you take a look at all the Building Blocks on SDL Tridion World (they contain a lot of example code which help you understand how to use the different APIs) or follow the available training's and don't forget the documentation (login required, see http://docportal.sdl.com/sdltridion for details on how to get access)

Bart Koopman
  • 4,835
  • 17
  • 30