2

I have already checked many post regarding getting component based on schema but I want to get the all component based on schema from folder and it's subfolder.I tried below solution but it still give those compoment which are having different schema from selected folder. If I removed "recursive=true" it worked but it not traverse in subfolder.

  OrganizationalItem organizationalItem = (OrganizationalItem)Engine.GetObject(Component.OrganizationalItem.Id);

        if (organizationalItem != null)
        {

            OrganizationalItemItemsFilter filter = new OrganizationalItemItemsFilter(organizationalItem.Session)
            {
                BaseColumns = ListBaseColumns.Extended,
                ItemTypes = new ItemType[] { ItemType.Component },
                Recursive = true
            };

            //Get the SchemaID from Parameter Schema.
            string SchemaIds = Package.GetValue("SchemaTcmId");

            List<Schema> lstSchema = new List<Schema>();
            if (!string.IsNullOrEmpty(SchemaIds))
            {

                Array.ForEach(SchemaIds.Split(",".ToCharArray()), s =>
                {
                    Schema currentitem;
                    currentitem = new Schema(new TcmUri(s), Engine.GetSession());
                    lstSchema.Add(currentitem);

                });

                Logger.Info(lstSchema[0].Id);
                filter.BasedOnSchemas = lstSchema;                                       
                XmlElement itemList = organizationalItem.GetListItems(filter);
                Package.AddXml("ComponentXml", itemList.OuterXml());
            }

        }
    }

Output : <tcm:ListItems Managed="10682" ID="tcm:5-14-2" xmlns:tcm="http://www.tridion.com/ContentManager/5.0"> <tcm:Item ID="tcm:5-85" Title="SDL launches profile-based marketing and e-commerce solution" Type="16" Modified="2011-02-23T05:41:24" From Pub="020 Content" IsNew="false" Icon="T16L0P1" SchemaId="tcm:5-83-8" SubType="0" IsPublished="true" Lock="0" IsShared="true" IsLocalized="false" Trustee="tcm:0-0-0" /> <tcm:Item ID="tcm:5-84" Title="The different levels of Compound Templating Part 1 of 3" Type="16" Modified="2011-01-31T19:52:40" FromPub="020 Content" IsNew="false" Icon="T16L0P1" SchemaId="tcm:5-83-8" SubType="0" IsPublished="true" Lock="0" IsShared="true" IsLocalized="false" Trustee="tcm:0-0-0" /> </tcm:ListItems>

2
  • Can you add all your code? I don't see where itemList is assigned.
    – robrtc
    Commented Dec 23, 2014 at 10:12
  • @robtc Updated missing code Commented Dec 23, 2014 at 11:16

2 Answers 2

2

I am using below code and that code gives the desired output which you are looking, please have a look.

Item itemComponent = package.GetByType(ContentType.Component);
                //get the attached component ID, through which we are got the path of the required folder. 
                Component component = (Component)engine.GetObject(itemComponent.GetAsSource().GetValue("ID"));
                PublicationTarget publicationTarget = engine.PublishingContext.PublicationTarget;

                OrganizationalItemItemsFilter orgFilter = new OrganizationalItemItemsFilter(engineSession);
                orgFilter.Recursive = true;
                orgFilter.ItemTypes = new[] { ItemType.Component };

                XmlElement List = component.OrganizationalItem.OrganizationalItem.GetListItems(orgFilter); // All component list 
                foreach (XmlNode node in List.ChildNodes)
                {
                    // leave the component whose ID is same as Dummy Component

                    fetchComponent = engine.GetObject(node.Attributes["ID"].Value) as Component;
                    if (fetchComponent.Schema.Title.ToLower() == "Schema name".ToLower())

In this scenario, I attached a component with the CT and then above code picks the attached component to get the desired location of your folder. I have created this attached component inside the desired folder of which components I want to read.

Just try this solution.

Thanks

3
  • Thanks Nash, but your solution don't have schema criteria to filter desired component from all component list. Commented Dec 23, 2014 at 13:58
  • If your folder holds almost similar kind of component(based on same schema) then you can filter the XmlElement list node. updated the code for filter as well
    – Nash
    Commented Dec 23, 2014 at 14:07
  • Thanks Nash , but if I have around 200 component in folder and getting each component and match schema title it's lots of time consuming.Actually if you see the output it contain SchemaId , If I put a check on SchemaId then I can get the desired XML of component based on schema but here is catch , In XML Localized SchemaId is shown and I am passing Parent SchemaId using parameter Schema in C# TBB. Any Thought ? Commented Dec 23, 2014 at 14:48
1

I would suggest using the Search API and a Search Criteria for getting the list of Components based on a Schema within a Folder.

I created a search and saved it as a search folder and have the following XML. You can either define a search folder in Tridion and get the list of items 'in' that folder or you could define your own search using the Search API. This assumed your search collection is up to date.

<SearchFolder xmlns="http://www.tridion.com/ContentManager/5.1/SearchFolder" xmlns:xlink="http://www.w3.org/1999/xlink">
    <GeneralParameters>
        <SearchQuery>*</SearchQuery>
        <SearchIn xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:24-98-2" Recursive="true" xlink:title="Content"></SearchIn>
    </GeneralParameters>
    <AdvancedParameters>
        <NumberOfItems>50</NumberOfItems>
        <BasedOnSchema>
            <Schema xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:24-430-8" xlink:title="Article"></Schema>
        </BasedOnSchema>
    </AdvancedParameters>
</SearchFolder>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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