从文件夹和子文件夹中读取所有组件

时间:2012-09-29 03:48:53

标签: tridion tridion2009

我正在使用.NET Templating C#2.0

开发Tridon 2009

我需要从文件夹及其子文件夹中读取所有组件。

如果在我的代码中,我写道:

 
OrganizationalItem imageFolder = 
    (OrganizationalItem)m_Engine.GetObject(comp.OrganizationalItem.Id);  

我能够从指示器组件所在的位置读取子文件夹中的所有组件,但是我无法读取指示符所在的文件夹中存在的其他组件。

但如果我写

OrganizationalItem imageFolder = (OrganizationalItem)m_Engine.GetObject(
                                comp.OrganizationalItem.OrganizationalItem.Id);  

然后我只能读取指示器组件所在的文件夹。

以下是我的代码。

XmlDocument doc = xBase.createNewXmlDocRoot("ImageLibrary");
XmlElement root = doc.DocumentElement;


Filter filter = new Filter();
Component comp = this.GetComponent();

filter.Conditions["ItemType"] = ItemType.Folder;
filter.Conditions["Recursive"] = "true";

OrganizationalItem imageFolder = 
       (OrganizationalItem)m_Engine.GetObject(comp.OrganizationalItem.Id);            
XmlElement itemList = imageFolder.GetListItems(filter);

foreach (XmlElement itemImg in itemList)
{
    filter.Conditions["ItemType"] = ItemType.Component;
    filter.Conditions["BasedOnSchema"] = comp.Schema.Id;

    OrganizationalItem imgFolder = 
       (OrganizationalItem)m_Engine.GetObject(itemImg.GetAttribute("ID")
       .ToString());
    XmlElement imageLibs = imgFolder.GetListItems(filter);

    doc = this.createImageNodes(imageLibs, doc, filter, comp);
    foreach (XmlElement imglib in imageLibsList)
    {
        XmlElement imageroot = doc.CreateElement("Image");
        XmlElement uploadeddateNode = doc.CreateElement("DateUploaded");
        Component imgComp =    
                (Component)m_Engine.GetObject(imglib.GetAttribute("ID"));
    }
}

请建议。

3 个答案:

答案 0 :(得分:6)

我在你的代码段上看到了很多关于“从文件夹和子文件夹中读取所有组件”的问题的多余代码

但是当你在做的时候回答这个问题:

OrganizationalItem imageFolder = (OrganizationalItem)m_Engine.GetObject(comp.OrganizationalItem.Id);

您无法读取该文件夹中的组件,因为您之前已将过滤器设置为仅在以下行中的文件夹:

filter.Conditions["ItemType"] = ItemType.Folder;

<强>解决方案:

如果要检索“指标组件”文件夹及其下方的所有组件,则需要在第一次搜索时设置过滤器,如下所示:

filter.Conditions["Recursive"] = "true";
filter.Conditions["ItemType"] = ItemType.Component;
filter.Conditions["BasedOnSchema"] = comp.Schema.Id;

执行搜索:

OrganizationalItem imageFolder = (OrganizationalItem)m_Engine.GetObject(comp.OrganizationalItem.Id);            
XmlElement itemList = imageFolder.GetListItems(filter);

答案 1 :(得分:2)

非常基本的东西。尽量避免使用Filter类,因为它在2009年已被弃用,并且尽可能多地使用GetListItems,因为获取列表总是更快。

public class GetComponentsInSameFolder : ITemplate
{
    public void Transform(Engine engine, Package package)
    {
        TemplatingLogger log = TemplatingLogger.GetLogger(GetType());
        if (package.GetByName(Package.ComponentName) == null)
        {
            log.Info("This template should only be used with Component Templates. Could not find component in package, exiting");
            return;
        }
        var c = (Component)engine.GetObject(package.GetByName(Package.ComponentName));
        var container = (Folder)c.OrganizationalItem;
        var filter = new OrganizationalItemItemsFilter(engine.GetSession()) { ItemTypes = new[] { ItemType.Component } };

        // Always faster to use GetListItems if we only need limited elements
        foreach (XmlNode node in container.GetListItems(filter))
        {
            string componentId = node.Attributes["ID"].Value;
            string componentTitle = node.Attributes["Title"].Value;
        }

        // If we need more info, use GetItems instead
        foreach (Component component in container.GetItems(filter))
        {
            // If your filter is messed up, GetItems will return objects that may
            // not be a Component, in which case the code will blow up with an
            // InvalidCastException. Be careful with filter.ItemTypes[]
            Schema componentSchema = component.Schema;
            SchemaPurpose purpose = componentSchema.Purpose;
            XmlElement content = component.Content;
        }
    }
}

答案 2 :(得分:0)

我认为你想要收集子文件夹并递归调用你的每个函数,这看起来就像你想要实现的那样。

这个函数叫做createImageNodes()吗?你在哪里设置imageLibsList?

看起来你在第一个循环中将每个项目视为一个文件夹,那些组件呢?