Documentation Center

Example handling Keywords (.NET)

The following example shows how to retrieve Keywords directly from a Publication or for each Category.

static void RetrievingKeywords(CoreServiceClient client)
{
	const string publicationUri = "tcm:0-3-1";

	// Get all keywords from all categories within a Publication
	XElement xmlKeywords = client.GetListXml(
		publicationUri,
		new RepositoryItemsFilterData { ItemTypes = new[] { ItemType.Keyword }, Recursive = true });

	IEnumerable<KeywordData> keywords =
		xmlKeywords.Elements().Select(element => element.Attribute("ID").Value).Select(id => (KeywordData)client.Read(id, null));
	foreach (KeywordData keyword in keywords)
	{
		Console.WriteLine("Keyword ID={0}, Title={1}", keyword.Id, keyword.Title);
	}

	// Get all categories within a Publication
	XElement xmlCategories = client.GetListXml(
		publicationUri, new RepositoryItemsFilterData { ItemTypes = new[] { ItemType.Category } });
	IEnumerable<CategoryData> categories =
		xmlCategories.Elements().Select(element => element.Attribute("ID").Value).Select(id => (CategoryData)client.Read(id, null));

	// for each category, get its keywords
	foreach (CategoryData category in categories)
	{
		Console.WriteLine("Category ID={0}, Title={1}", category.Id, category.Title);

		XElement xmlCategoryKeywords = client.GetListXml(category.Id, new KeywordsFilterData());
		IEnumerable<KeywordData> categoryKeywords =
			xmlCategoryKeywords.Elements().Select(element => element.Attribute("ID").Value).Select(id => (KeywordData)client.Read(id, null));
		foreach (KeywordData keyword in categoryKeywords)
		{
			Console.WriteLine("\t Keyword ID={0}, Title={1}", keyword.Id, keyword.Title);
		}
	}
}