3

I am trying to get the Components based on a specific Schema in my TBB. But every time it is returning all the Components created under Building Blocks. I am putting my code below. Any help will be really appreciated

Page page = _utilities.GetPage();
Publication publication = GetPublication(page);

Component component = page.ComponentPresentations[0].Component;
Schema videoSchema = component.Schema;

var filter = new OrganizationalItemItemsFilter(_engine.GetSession());
filter.Recursive = true;
filter.ItemTypes = new[] { ItemType.Component };
filter.BasedOnSchemas = new[] { videoSchema };
filter.ComponentTypes = new[] { ComponentType.Normal };
var testComponents = publication.RootFolder.GetItems(filter);

3 Answers 3

2

Using WhereUsed has certain limitations. If you use InRepository as current Publication, it will only return the Components that are either localized or created in that Publication.

If you exclude the above criteria you will get a lot of duplicate values. That will make your situation more complicated. In my view it would be easier to use an iterative method to traverse the Folders and get the list of Components.

var container = (Folder)component.OrganizationalItem;
var filter = new OrganizationalItemItemsFilter(engine.GetSession()) { ItemTypes = new[] { ItemType.Component }, BasedOnSchemas = new[] { schema } };
foreach (XmlNode node in container.GetListItems(filter))
{
    string componentId = node.Attributes["ID"].Value;
    string componentTitle = node.Attributes["Title"].Value;
}
2

Your schema variable name videoSchema suggests like you are looking for multimedia Components.

If this is the case try below:

filter.ComponentTypes = new[] { ComponentType.Multimedia}; 

Update:

Your code looks correct to me, I've read in a post that the conditions Recursive and BasedOnSchemas don't work together. So may be you can try iterating through the Folders and get the Components based on the Schema and make a collection of the Components.

1
  • Actually it is not a multimedia component. it is a normal component. I am sorry if the schema name makes any confusion.
    – Ken
    Commented Jun 12, 2015 at 16:26
1

Using BasedOnSchemas and Recursive=true isn't supported.

If you remove Recursive=true you will see the schema filter works as expected.

Your other option for doing a deeper look into the schema with a ShowWhereUsed although this is a heavier query so you need to consider the user experience here.

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.