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>