I've just started using uTaxonomy - looks great by the way. I've noticed that when I save a selection of tags it's saved in the format [ " GUID ", " GUID " ]. I find that I have to strip out the square brackets and the quotes in order to use the conversion functions in the Taxonomy.cs via RegEx.
Am I missing a trick here? Or is there a function that does this automatically.
In addition, I'm looking to Index the field in Examine but because it includes the Square Brackets and quotes when you save - I can't do a faceted search.
I added TaxonomyValueConverter in the Taxonomy.cs file.
You can use GetPropertyValue<> to get taxonomy items.
For example: var taxonomyItems = Model.Content.GetPropertyValue<List<Taxonomy>>("testTaxonomy");
where testTaxonomy is property name.
If you need to get only guid list, you can use JsonConvert (Newtonsoft.Json package) to deserialize json object to guid list: List<Guid> result = JsonConvert.DeserializeObject<List<Guid>>(sourceString);
Where sourceString is string: [ "2a1bf120-d77e-44aa-8a1b-919c33e2f7bd", "dd60432f-3903-43d2-b47b-34bc6cac7895", "242c7542-9f39-4d17-a3b1-a03d1a65a0d5" ]
About Examine. At the moment we do not have the correct decision, but we think. We have received a proposal to preserve not only the guid but also the name of the element. But this solution implies problem. If someone changes the name of the item in the Data Type and does not save content with the new values, then Examine will contain the old values for the names. That is why we maintain only the guid, as they remain unique when you change the name of the element.
As for examine solution I would suggest you to manually extend the index by handling BaseIndexProvider.GatheringNodeData event. Sample implementation of the event handler could be the following (assume there are stored location tags in the Umbraco content and examine configured to index that property)
private void OnGatheringNodeData(object sender, IndexingNodeDataEventArgs e)
{
// logging stuff
LogHelper.Info<IndexExtender>("Extending index node: IndexType={0}, Indexer={1}, NodeId={2}", () => e.IndexType, () => _indexer.Name, () => e.NodeId);
try
{
// retrieve json array stored in the examine index field, which is configurated to index content property of 'uTaxonomy'
var locationsJson = e.Fields["locations"];
// deserialize raw json array to the list of strings
var locationIds = JsonConvert.DeserializeObject<List<string>>(locationsJson);
var locationNames = new List<string>();
// iterate over the identifiers to retrieve taxonomy names
foreach(var locId in locationIds)
{
// "Locations Taxonomy" - Umbraco uTaxonomy data type
Taxonomy location = TaxonomyService.GetTaxonomyById("Locations Taxonomy", locId);
locationNames.Add(location.ToString());
}
// produce a string containing location names separated by space to make it available for searching and put the string into the new examine field
e.Fields["locations_searcheable"] = string.Join(" ", locationNames);
// for now its possible to search by location name against "locations_searcheable" examine field
}
catch (Exception ex)
{
// logging stuff
LogHelper.Error<IndexExtender>(string.Format("Extending index node failed: IndexType={0}, Indexer={1}, NodeId={2}", e.IndexType, _indexer.Name, e.NodeId), ex);
}
}
_indexer; private readonly BaseIndexProvider _indexer; (Examine.Providers namespace)
and _indexer is initialized in the constructor:
protected IndexExtender(string indexerName)
{
_indexer = ExamineManager.Instance.IndexProviderCollection[indexerName];
}; where indexerName is index name from ExamineSettings.config.
For example:
I've got an issue that I'm trying to work out, basically the GetTaxonomyTree doesn't seem to show the child items.
Here's my code/your code:-
var result = new List<Taxonomy>();
var dataTypeService = UmbracoContext.Current.Application.Services.DataTypeService;
var dataType = dataTypeService.GetDataTypeDefinitionByName("Areas");
if (dataType != null)
{
var valueList = dataTypeService.GetPreValuesCollectionByDataTypeId(dataType.Id);
if (valueList != null)
{
var value = valueList.PreValuesAsDictionary.FirstOrDefault(x => x.Key == "tree");
if (value.Value != null)
{
result = JsonConvert.DeserializeObject<List<Taxonomy>>(value.Value.Value);
foreach (var itm in result) {
Response.Write(itm.Item.Id + "<br/>");
}
}
}
}
This only outputs the top level items in the list - how do we get the children to show?
To convert a json object to the desired structure, we use the library Newtonsoft.Json. She analyzes the Json object and converts it to the desired structure.
During this transformation corresponds to a string:
result = JsonConvert.DeserializeObject<List<Taxonomy>>(value.Value.Value);
The result returns a list, and each item in the list is the field of Children and if there are child elements, then they will be in this field.
Output of GUIDs List
Hello,
I've just started using uTaxonomy - looks great by the way. I've noticed that when I save a selection of tags it's saved in the format [ " GUID ", " GUID " ]. I find that I have to strip out the square brackets and the quotes in order to use the conversion functions in the Taxonomy.cs via RegEx.
Am I missing a trick here? Or is there a function that does this automatically.
In addition, I'm looking to Index the field in Examine but because it includes the Square Brackets and quotes when you save - I can't do a faceted search.
Any tips on how to get around this.
Many thanks
Paul
Hi Paul,
I released new version (0.8.1)
var taxonomyItems = Model.Content.GetPropertyValue<List<Taxonomy>>("testTaxonomy");
where testTaxonomy is property name.List<Guid> result = JsonConvert.DeserializeObject<List<Guid>>(sourceString);
Where sourceString is string:[ "2a1bf120-d77e-44aa-8a1b-919c33e2f7bd", "dd60432f-3903-43d2-b47b-34bc6cac7895", "242c7542-9f39-4d17-a3b1-a03d1a65a0d5" ]
Thanks,
Vitaly
Hi Vitaly,
Thanks for this, I'll give it a go.
Cheers
Paul
Hi Paul,
As for examine solution I would suggest you to manually extend the index by handling
BaseIndexProvider.GatheringNodeData
event. Sample implementation of the event handler could be the following (assume there are stored location tags in the Umbraco content and examine configured to index that property)HTH, Artyom
Hi Artyom,
Thanks for this, can you tell me what the namespace is for IndexExtender from the line "LogHelper.Info
Thanks
Paul
Hi Paul,
LogHelper is in Umbraco.Core.Logging namespace;
_indexer;
private readonly BaseIndexProvider _indexer;
(Examine.Providers namespace) and _indexer is initialized in the constructor:protected IndexExtender(string indexerName) { _indexer = ExamineManager.Instance.IndexProviderCollection[indexerName]; };
where indexerName is index name from ExamineSettings.config. For example:<add name="ApplicationIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine" supportUnpublished="false" supportProtected="true" analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"/>
Best regards,
Vitaly
Hello again and Happy new year.
I've got an issue that I'm trying to work out, basically the GetTaxonomyTree doesn't seem to show the child items.
Here's my code/your code:-
This only outputs the top level items in the list - how do we get the children to show?
Many Thanks
Paul
Hi Paul,
Sorry that was so long to reply. Holidays.
You're wrong.
To convert a json object to the desired structure, we use the library Newtonsoft.Json. She analyzes the Json object and converts it to the desired structure.
During this transformation corresponds to a string:
The result returns a list, and each item in the list is the field of Children and if there are child elements, then they will be in this field.
Best regads, Vitaly
Thank Vitaly, this is great. I think I've managed to clear up all my code issues now.
Many thanks for all your help.
Paul
You are welcome.
Vitaly
is working on a reply...