2

I am new to template building blocks. I need to get all the components from a publication based on a particular schema inside a .Net TBB.

Can you please help me with the piece of code.

update

I applied @Andreys solution. But some how it is returning all the Components. Here is my code. Can you please take a look?

Page page = page = _utilities.GetPage(); 
Publication publication = GetPublication(page); 
Component component = page.ComponentPresentations[0].Component; 
Schema videoSchema = (Schema)_engine.GetObject(component.Schema.Id); 
var filter = new OrganizationalItemItemsFilter(_engine.GetSession()); 
filter.Recursive = true; 
filter.ItemTypes = new[] { ItemType.Component }; 
filter.BasedOnSchemas = new[] { videoSchema }; 
var components = publication.RootFolder.GetItems(filter);
4

The code provided by Saurabh is using the obsolete Filter class. Here's the better way:

var filter = new OrganizationalItemItemsFilter(session);
filter.Recursive = true;
filter.ItemTypes = new[] { ItemType.Component };
filter.BasedOnSchemas = new[] { schema };

var components = publication.RootFolder.GetItems(filter);

This way is a also a tiny bit lighter, as there can't be components in Strucuture Groups or in Categories, it does make sense to look only in the Building Blocks folder.

  • I applied your solution. But some how it is returning all the components. Here is my code. Can you please take a look... – Ken Jun 12 '15 at 14:57
  • Page page = page = _utilities.GetPage(); Publication publication = GetPublication(page); Component component = page.ComponentPresentations[0].Component; Schema videoSchema = (Schema)_engine.GetObject(component.Schema.Id); var filter = new OrganizationalItemItemsFilter(_engine.GetSession()); filter.Recursive = true; filter.ItemTypes = new[] { ItemType.Component }; filter.BasedOnSchemas = new[] { videoSchema }; var components = publication.RootFolder.GetItems(filter); – Ken Jun 12 '15 at 14:58
2

I've used some code like below long time back, hope this helps.

                Filter fltr = new Filter();
                fltr.Conditions["Recursive"] = "true";
                fltr.Conditions["ItemType"] = ItemType.Component;
                fltr.Conditions["BasedOnSchema"] = "{tcmId of schema}";
                var components = publication.GetItems(fltr);

I copied this code from version 2009 days. So yeah as Andrey mentioned in his answer, the the Filter Class is Obselete now. So use OrganizationalItemItemsFilter if your tridion version is higher than 2009.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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