Setting the value of HierarchicalTaxonField
I'm using a HierarchicalTaxonField in a custom module's web ui. I can read the value of it and save it to my db table without any issues, but I cannot bind a value back to it on my edit screen.
Setting the field's value to any IList<Guid> or TackedList<Guid> on page load does nothing. Literally. I can set the value to a list of guids, and inspecting the value on the very next line shows that it has not changed, as if the setter on the property doesn't exist.
I can kind of make this work if I set BindOnServer to false, but then I can't read the value back on form submission.
This seems like it should be trivial to do but I can't find any examples of how to do this. You should be able to use this field on an item's edit screen without any problems, so why is it so difficult?
Hello Chris,
Try using the item.Organizer.AddTaxa("fieldName", taxonID) in order to associate a taxon with a certain item. Here is a sample that does this with Documents and custom taxonomy.
LibrariesManager libManager = LibrariesManager.GetManager();
TaxonomyManager taxManager = TaxonomyManager.GetManager();
var master = libManager.GetDocuments().Where(d => d.Title ==
"DocumentTitle"
&& d.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master).FirstOrDefault();
var taxon = taxManager.GetTaxa<HierarchicalTaxon>().FirstOrDefault(s => s.Title ==
"TaxonTitle"
);
if
(master !=
null
)
var temp = libManager.Lifecycle.CheckOut(master)
as
Document;
temp.Organizer.AddTaxa(
"FieldTitle"
, taxon.Id);
master = libManager.Lifecycle.CheckIn(temp)
as
Document;
libManager.SaveChanges();
Hi Pavel,
I'm not trying to associate a taxon with an item in this case. I'm trying to set the value of a HierarchicalTaxonField control on the edit page for a custom content type when the page loads.
TrackedList<Guid> categories = GetCategoriesForItem();
// I'm deserializing Guids from a string here
// categories now contains two guids,
// categoriesSelector.Value is an empty TrackedList<Guid>
categoriesSelector.Value = categories;
// set the value of the control to the saved categories