Example: retrieving items from .NET using the Core Service
Thie sizable example gives an impression on how to use the list retrieval methods and filters.
Call the GetListXml() and GetSystemWideList() methods from the two Core Service interfaces, ICoreService or ISessionAwareCoreService, to retrieve items and to create lists of items within a Content Manager container, such as a Folder or a Category. You can use these methods to build navigation structures or automatic lists.
GetListXml() returns a DOM object containing the XML equivalent of the objects returned by GetSystemWideList(). If you only need the properties of the items, GetListXml() gives you much better performance. You can also select which attributes to include or exclude in the XML string, and filter the items returned.
GetListXml() takes two parameter: the ID (either a Content Manager URI or WebDAV URL) of the organizational item from with you want to retrieve items, and a filter (which restricts which items are returned). Filters are subclasses of the class SubjectRelatedListFilterData in the Tridion.ContentManager.Data namespace. Each filter class name ends in the strings FilterData.
The following example shows some simple examples of retrieving items using filters.
static void RetrievingItems(CoreServiceClient client)
{
const string publicationUri = "tcm:0-3-1";
// One way of retrieving the root Structure Group:
PublicationData publication = (PublicationData)client.Read(publicationUri, null);
Console.WriteLine("Root structure group: {0}", publication.RootStructureGroup.IdRef);
// Another way of retrieving the root Structure Group, without reading the Publication:
// Get only the Structure Groups of the Publication
XElement structureGroups = client.GetListXml(
publicationUri,
new RepositoryItemsFilterData { ItemTypes = new[] { ItemType.StructureGroup } });
// Get the ID (Content Manager URI) of the first Structure Group
string firstStructureGroupUri = (
from e in structureGroups.Elements()
where e.Name.LocalName == "Item"
select e.Attribute("ID").Value
).First();
Console.WriteLine("Root structure group: {0}", firstStructureGroupUri);
// Now that we have the Structure Group URI, get only the Pages of the Structure Group
XElement pages = client.GetListXml(
firstStructureGroupUri,
new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType.Page } });
Console.WriteLine("Pages in the Structure Group:");
foreach (XElement page in pages.Elements())
{
string pageUri = page.Attribute("ID").Value;
Console.WriteLine("\t Page {0}", pageUri);
Console.WriteLine("\t Used in:");
XElement usingItems = client.GetListXml(pageUri, new UsingItemsFilterData());
foreach (XElement usingItem in usingItems.Elements())
{
string whereUsedUri = usingItem.Attribute("ID").Value;
Console.WriteLine("\t\t {0}", whereUsedUri);
}
}
}