1

我正在使用 .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"));
    }
}

请建议。

4

3 回答 3

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);
于 2012-09-29T10:58:40.837 回答
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;
        }
    }
}
于 2012-10-01T19:55:39.987 回答
0

我认为您想收集子文件夹并为每个子文件夹递归调用您的函数,这似乎是您想要实现的目标。

这个函数是否称为 createImageNodes(),你在哪里设置 imageLibsList?

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

于 2012-09-29T04:55:50.580 回答