How to get HierarchicalTaxonField values to string for parsing
I have a widget details template with a HierarchicalTaxonField in it displaying "categories" assigned to the content item. How can I get the output of this field into a string for parsing etc?
Here's the answer to my own question for anyone who ever wants to do this. I know there have been several inquiries but no good full solution. In this particular case, we're using an Event item. This code will get you the a comma delimited string of category names.
Protected Sub singleItemContainer_ItemDataBound(sender As Object, e As Telerik.Web.UI.RadListViewItemEventArgs) Handles singleItemContainer.ItemDataBound
Dim item As RadListViewDataItem = TryCast(e.Item, RadListViewDataItem) 'create from current item item
Dim evt As Events.Model.Event = DirectCast(item.DataItem, Events.Model.Event) 'create an Event object from item
Dim tm As New TaxonomyManager() 'taxa manager
Dim keywords = New List(Of String)() 'where we'll store the list of categories
Dim cats = DirectCast(evt.GetValue("Category"), Telerik.OpenAccess.TrackedList(Of System.Guid)) 'gets a list of category guids from current event
If cats.Count > 0 Then
For Each g As Guid In cats 'loops through to get cat names from Guids via taxa manager
Dim ht As HierarchicalTaxon = DirectCast(tm.GetTaxon(g), HierarchicalTaxon)
If ht IsNot Nothing AndAlso Not keywords.Contains(ht.Name.ToLowerInvariant()) Then
keywords.Add(ht.Name.ToLowerInvariant()) 'adds cat name to keywords list
End If
Next
End If
Dim catsList As String = String.Empty
For Each cat As String In keywords 'parses keywords to build string of cat names
catsList = catsList & cat & ","
Next
End Sub
Hi Craig,
If I understand properly, you want to display all categories, which are assigned to the item, under the item. Here's how you can do this. Let's say you're displaying the items in a RadListView, as in our default toolbox widgets. On ItemDataBound you can get the dataItem and all categories, assigned to it and generate a link, which will be placed under the item. It will display the name of the categories and in the same time will be a filtering link - when you click on it it will filter all items by the corresponding category. Please take a look at the implementation of the RenderHierarchicalTaxaAsLink method.
void
NewsList_ItemDataBound(
object
sender, Telerik.Web.UI.RadListViewItemEventArgs e)
switch
(e.Item.ItemType)
case
RadListViewItemType.DataItem:
case
RadListViewItemType.AlternatingItem:
var listViewDataItem = (RadListViewDataItem)e.Item;
var dataItem = (NewsItem)listViewDataItem.DataItem;
if
(dataItem !=
null
)
var placeholder = e.Item.FindControl(
"CategoriesLabel"
)
as
SitefinityLabel;
placeholder.Text = RenderHierarchicalTaxaAsLink(dataItem,
"Category"
);
break
;
public
string
RenderHierarchicalTaxaAsLink(NewsItem newsitem,
string
taxonomyFieldName)
var catList =
""
;
var categories = newsitem.GetValue<TrackedList<Guid>>(taxonomyFieldName);
if
(categories !=
null
&& categories.Count() > 0)
catList =
"Categories:</br> "
;
var currentUrl = HttpContext.Current.Request.Url.ToString();
if
(currentUrl.EndsWith(
"/"
))
currentUrl = currentUrl.TrimEnd(
'/'
);
var taxManager = TaxonomyManager.GetManager();
foreach
(var cat
in
categories)
var t = taxManager.GetTaxon<HierarchicalTaxon>(cat);
var url =
string
.Format(
"0/-in-1/2/3"
,currentUrl, t.Taxonomy.TaxonName, t.Taxonomy.Name, t.Name);
var link =
string
.Format(
"<a href='0'>1</a><br/>"
, url, t.Name).ToString();
catList = catList + link;
return
catList;