Getting Using and Used Items With TOM.NET

Periodically I will receive an e-mail from someone in the Tridion community asking questions like “How do I get the pages where a component is used?” or “How do I get all the components using a particular schema?”. Although there is already some good examples out there on retrieving Where Using and Where Used, I figured some more Tridion samples out there could never hurt.

GetUsingItems

The Tridion.ContentManager.IdentifiableObject (the base class that most of the other items in Tridion inherits) has a method called “GetUsingItems” that allows you to get all items using this item. Note that these are the items you see in the “Used in” tab when you use the “Where Used” functionality in the GUI. The UsingItemsFilter argument allows you to refine your query of using items even further.


IdentifiableObject.GetUsingItems()
IdentifiableObject.GetUsingItems(UsingItemsFilter filter)

Getting all pages used by a component:

UsingItemsFilter filter = new UsingItemsFilter(component.Session);
filter.ItemTypes = new List<ItemType> { ItemType.Page };
filter.IncludedVersions = VersionCondition.OnlyLatestVersions;

IEnumerable<IdentifiableObject> pages = component.GetUsingItems(filter);

Getting all components used by a schema:

UsingItemsFilter filter = new UsingItemsFilter(schema.Session);
filter.ItemTypes = new List<ItemType> { ItemType.Component };
filter.IncludedVersions = VersionCondition.OnlyLatestVersions;
filter.InRepository = (Repository)publication; // assuming variable publication is of type Tridion.ContentManager.CommunicationManagement.Publication and the only publication you want the results to return components for

IEnumerable<IdentifiableObject> components = schema.GetUsingItems(filter);

Getting all schemas that used a given embedded schema:

UsingItemsFilter filter = new UsingItemsFilter(embeddedSchema.Session);
filter.ItemTypes = new ItemType[] { ItemType.Schema }; // Note can be an array too
filter.IncludedVersions = VersionCondition.OnlyLatestVersions;

IEnumerable<Schema> schemas = (IEnumerable<Schema>)embeddedSchema.GetUsingItems(filter);

Getting all components using a gien keyword:

UsingItemsFilter filter = new UsingItemsFilter(keyword.Session);

filter.ItemTypes = new List<ItemType> { ItemType.Component };
filter.IncludedVersions = VersionCondition.OnlyLatestVersions;

IEnumerable<IdentifiableObject> components = keyword.GetUsingItems(filter);

Note that the above could have just been retrieved using the keyword’s GetClassifiedItems() method:

IEnumerable<RepositoryLocalObject> components = keyword.GetClassifiedItems();

GetUsedItems

The “GetUsedItems” method of IdentifiableObject works very similarly to the “GetUsingItems” method, but in reverse: it’ll grab items that this item uses. This is the same functionality as the “Uses” tab in the “Where Used” functionality of the GUI.


IdentifiableObject.GetUsedItems();
IdentifiableObject.GetUsedItems(UsedItemsFilter filter);

Get the embedded schemas used in a given schema:

UsedItemsFilter filter = new UsedItemsFilter(schema.Session);
filter.ItemTypes = new ItemType[] { ItemType.Schema };

IEnumerable<IdentifiableObject> embeddedSchemas = schema.GetUsedItems(filter);

Get Components Within ComponentLinks of a given component:

UsedItemsFilter filter = new UsedItemsFilter(component.Session);
filter.ItemTypes = new ItemType[] { ItemType.Component };

var compLinks = component.GetUsedItems(filter);

Getting the List as XML

When working with a pretty large set of items that may get returned, you may want to work with an XML of the used items instead for performance reasons. The following methods return a type of XmlElement:


IdentifiableObject.GetListUsedItems();
IdentifiableObject.GetListUsedItems(UsedItemsFilter filter);
IdentifiableObject.GetListUsingItems();
IdentifiableObject.GetListUsingItems(UsingItemsFilter filter);