【问题标题】:Reading all components from folder and subfolder从文件夹和子文件夹中读取所有组件
【发布时间】:2012-09-29 03:48:52
【问题描述】:

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

请提出建议。

【问题讨论】:

    标签: tridion tridion2009


    【解决方案1】:

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

    但是在你做的时候回答问题本身:

    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);
    

    【讨论】:

    • 非常感谢。今天,我没有时间解决这个问题。我将对此进行测试并回复您。再次感谢
    【解决方案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;
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

      • 是的,我需要阅读我的指标组件所在的所有组件。此外,阅读文件夹中的所有组件以及存在指标的子文件夹。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      相关资源
      最近更新 更多