tridiontridion-2011

How to get Categories from Tridion using categoriesdata


I was looking for a way that how can I fetch all the categories from Tridion using Categoriesdata. I was trying like below, but its showing error.

CoreServiceSession client = new CoreServiceSession();
SessionAwareCoreServiceClient csClient = client.GetClient();
ReadOptions readoption = new ReadOptions();

CategoriesFilterData filter = new CategoriesFilterData();
XElement xml = csClient.GetSystemWideList(filter);

Solution

  • You need to use GetListXml instead of GetSystemWideList and specify the publication ID from which you want to retrieve the categories:

    CategoriesFilterData filterData = new CategoriesFilterData();
    XElement resultXml = client.GetListXml(publicationId, filterData);
    

    GetSystemWideList is usually for retrieving stuff that is system wide and not bound to just 1 publication, like PublicationTargets and MultimediaTypes

    You could however also try a Search query, like so:

    SearchQueryData filter = new SearchQueryData(); 
    filter.ItemTypes = new ItemType[] { ItemType.Category }; 
    
    IdentifiableObjectData[] results = client.GetSearchResults(filter);
    foreach (IdentifiableObjectData obj in results)
    {
        Console.WriteLine(String.Format("{0} - {1}", obj.Title, obj.Id));
    }