Skip to main content
Bounty Ended with 50 reputation awarded by Rakesh Choudhary
deleted 4 characters in body
Source Link

AFAIK, you won't be able to get the Keyword titles directly in a Razor TBB; however, you can create your own custom function in the Razor TBB that manipulates the XML and returns the titles.

The example below is a custom function that takes in as parameter a category XML string and returns the Keyword titles as a list:

@using System;
@using System.Collections.Generic;
@using System.Linq;
@using System.Xml;
@using System.Xml.Linq;
@using Tridion.ContentManager.Templating;

@functions 
{
    public List<string> GetKeywordsTitleFromCategoryXML(string categoryXml) 
    {
        XDocument xDocument = XDocument.Parse(categoryXml);
        XNamespace ns = XNamespace.Get("http://www.tridion.com/ContentManager/5.0");
        var list = xDocument.Descendants(ns + "Item").Attributes("Title").ToList();

        List<string> keywordTitles = new List<string>();

        foreach (String keywordTitle in list)
        {
            keywordTitles.Add(keywordTitle);
        }

        return keywordTitles;
    }
}

Then, you could do something like this to get render the Keyword titles:

@{
    var categoryXmlString = @Package.GetByName("MyKeywords").GetAsXmlDocument().OuterXml;
    var keywordTitleList = @GetKeywordsTitleFromCategoryXML(categoryXmlString);
}

@foreach(var keywordTitle in keywordTitleList)
{
   <p>@keywordTitle</p>
}

AFAIK, you won't be able to get the Keyword titles directly in a Razor TBB; however, you can create your own custom function in the Razor TBB that manipulates the XML and returns the titles.

The example below is a custom function that takes in as parameter a category XML string and returns the Keyword titles as a list:

@using System;
@using System.Collections.Generic;
@using System.Linq;
@using System.Xml;
@using System.Xml.Linq;
@using Tridion.ContentManager.Templating;

@functions 
{
    public List<string> GetKeywordsTitleFromCategoryXML(string categoryXml) 
    {
        XDocument xDocument = XDocument.Parse(categoryXml);
        XNamespace ns = XNamespace.Get("http://www.tridion.com/ContentManager/5.0");
        var list = xDocument.Descendants(ns + "Item").Attributes("Title").ToList();

        List<string> keywordTitles = new List<string>();

        foreach (String keywordTitle in list)
        {
            keywordTitles.Add(keywordTitle);
        }

        return keywordTitles;
    }
}

Then, you could do something like this to get render the Keyword titles:

@{
    var categoryXmlString = @Package.GetByName("MyKeywords").GetAsXmlDocument().OuterXml;
    var keywordTitleList = @GetKeywordsTitleFromCategoryXML(categoryXmlString);
}

@foreach(var keywordTitle in keywordTitleList)
{
   <p>@keywordTitle</p>
}

AFAIK, you won't be able to get the Keyword titles directly in a Razor TBB; however, you can create your own custom function in the Razor TBB that manipulates the XML and returns the titles.

The example below is a custom function that takes in as parameter a category XML string and returns the Keyword titles as a list:

@using System;
@using System.Collections.Generic;
@using System.Linq;
@using System.Xml;
@using System.Xml.Linq;
@using Tridion.ContentManager.Templating;

@functions 
{
    public List<string> GetKeywordsTitleFromCategoryXML(string categoryXml) 
    {
        XDocument xDocument = XDocument.Parse(categoryXml);
        XNamespace ns = XNamespace.Get("http://www.tridion.com/ContentManager/5.0");
        var list = xDocument.Descendants(ns + "Item").Attributes("Title").ToList();

        List<string> keywordTitles = new List<string>();

        foreach (String keywordTitle in list)
        {
            keywordTitles.Add(keywordTitle);
        }

        return keywordTitles;
    }
}

Then, you could do something like this to render the Keyword titles:

@{
    var categoryXmlString = @Package.GetByName("MyKeywords").GetAsXmlDocument().OuterXml;
    var keywordTitleList = @GetKeywordsTitleFromCategoryXML(categoryXmlString);
}

@foreach(var keywordTitle in keywordTitleList)
{
   <p>@keywordTitle</p>
}
Source Link

AFAIK, you won't be able to get the Keyword titles directly in a Razor TBB; however, you can create your own custom function in the Razor TBB that manipulates the XML and returns the titles.

The example below is a custom function that takes in as parameter a category XML string and returns the Keyword titles as a list:

@using System;
@using System.Collections.Generic;
@using System.Linq;
@using System.Xml;
@using System.Xml.Linq;
@using Tridion.ContentManager.Templating;

@functions 
{
    public List<string> GetKeywordsTitleFromCategoryXML(string categoryXml) 
    {
        XDocument xDocument = XDocument.Parse(categoryXml);
        XNamespace ns = XNamespace.Get("http://www.tridion.com/ContentManager/5.0");
        var list = xDocument.Descendants(ns + "Item").Attributes("Title").ToList();

        List<string> keywordTitles = new List<string>();

        foreach (String keywordTitle in list)
        {
            keywordTitles.Add(keywordTitle);
        }

        return keywordTitles;
    }
}

Then, you could do something like this to get render the Keyword titles:

@{
    var categoryXmlString = @Package.GetByName("MyKeywords").GetAsXmlDocument().OuterXml;
    var keywordTitleList = @GetKeywordsTitleFromCategoryXML(categoryXmlString);
}

@foreach(var keywordTitle in keywordTitleList)
{
   <p>@keywordTitle</p>
}