1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to display custom taxonomy value

How to display custom taxonomy value

7 posts, 0 answered
  1. John
    John avatar
    61 posts
    Registered:
    08 Jun 2012
    11 Jul 2011
    Link to this post
    Hello,

    We are creating a custom module that is based on the products example. Similar to the "Colors" taxonomy created in the products example, we are adding two custom taxonomies: DataType and ValueType to a LocalizedText object. We have also added a "Key" and "ValueText" fields to our LocalizedText item Model, as per the products example (i.e. like Price, or QuantityInStock in that example).

    We have successfully created the module and view code that creates and saves these items. My questions is: how do we access the assigned custom taxonomy values programmatically? The DataType and ValueType values do not have corresponding properties on the LocalizedText object. Ideally, we could just access these like localizedText.DataType, or localizedText.ValueType.

    Thank you for any insight!

    Regards,
    John
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    12 Jul 2011
    Link to this post
    Hello John,

    Taxonomies could be used only in the context of the Content item (Telerik.Sitefinity.GenericContent.Model) and its inheritors. Programmatically you can get the taxonomy using TypeDescriptior.GetProperties or DataExtension method - GetValue

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. John
    John avatar
    61 posts
    Registered:
    08 Jun 2012
    19 Jul 2011
    Link to this post
    Hello Ivan,

    Thank you!

    Basically, I still cannot figure out how to *set* (assign?) a taxon value to a content-based OpenAccess data item.

    Here is what I've tried:

    - Create content-based module
    - Created custom taxonomy named "ValueTypes" with "PlainText" and "Html" taxons
    - Added the meta type/fields to my module type (meta field named "ValueType")
    - Installed the module with no apparent problems: Taxonomy and Taxa were all created fine, I can see them in the database. Meta Types and Fields were all created.

    If I understand your instructions correctly, I should be able to access the assigned taxonomy on one of my items like this:

      item.GetValue("ValueType")

    Is that correct? Do I cast that as a Taxon to reference it?

    Also, I am trying to update the field like this:

    - get the taxon object I want to add (taxonPlainText)
    - try to set this on my item:  item.SetValue("ValueType", taxonPlainText);

    When I try that, I am geting an exception:
    System.InvalidCastException: Object must implement IConvertible.

    on the SetValue line. I found an example that shows the following:
    item.Organizer.SetTaxon("ValueType", taxonPlainText.Id)

    I tried this as well, but I get the following exception:
    The specified property 'DataType' must be of type 'System.Guid'
    which kind of makes sense, since I am passing a Guid value to set. 

    So, I am stuck - I cannot figure out how to assign a custom taxonomy value to my content-based items.

    Thank you for any insight!

    For reference, here is my full provider method (where everything works except the taxonomy updates). This is mostly duplicated from the Products module example:
    01.public override Country CreateCountry(Guid id)
    02.{
    03.    var country = new Country();
    04.    country.Id = id;
    05.    country.ApplicationName = this.ApplicationName;
    06.    country.Owner = SecurityManager.GetCurrentUserId();
    07.    var dateValue = DateTime.UtcNow;
    08.    country.DateCreated = dateValue;
    09.    country.PublicationDate = dateValue;
    10.    ((IDataItem)country).Provider = this;
    11. 
    12.    // Assign custom taxonomy values for Data Type (i.e. "Country") and Value Type (i.e. "PlainText")
    13.    Taxon dataTypeTaxon = this.GetDataTypeTaxon(LocalizedDomainDataDataTypes.Country);
    14.    if (dataTypeTaxon != null)
    15.    {
    16.        //country.SetValue(LocalizedDomainDataModule.DataTypeTaxonName, dataTypeTaxon);
    17.        country.Organizer.SetTaxon(LocalizedDomainDataModule.DataTypeTaxonName, dataTypeTaxon.Id);
    18.    }
    19.    else
    20.    {
    21.        throw new Exception("Unknown Jajah Localized Domain Data Type : '" + LocalizedDomainDataDataTypes.Country.ToString() + "'");
    22.    }
    23. 
    24.    Taxon valueTypeTaxon = this.GetValueTypeTaxon(LocalizedDomainDataValueTypes.PlainText);
    25.    if (valueTypeTaxon != null)
    26.    {
    27.        //country.SetValue(LocalizedDomainDataModule.ValueTypeTaxonName, valueTypeTaxon);
    28.        country.Organizer.SetTaxon(LocalizedDomainDataModule.ValueTypeTaxonName, valueTypeTaxon.Id);
    29.    }
    30.    else
    31.    {
    32.        throw new Exception("Unknown Jajah Localized Domain Data Value Type : '" + LocalizedDomainDataValueTypes.PlainText.ToString() + "'");
    33.    }
    34. 
    35.    // Country permissions inherit from the security root
    36.    var securityRoot = this.GetSecurityRoot();
    37.    if (securityRoot != null)
    38.    {
    39.        this.providerDecorator.CreatePermissionInheritanceAssociation(securityRoot, country);
    40.    }
    41.    else
    42.    {
    43.        var msg = Res.Get<SecurityResources>().NoSecurityRoot;
    44.        msg = string.Format(msg, typeof(Country).AssemblyQualifiedName);
    45.        throw new InvalidOperationException(msg);
    46.    }
    47. 
    48.    // items with empty guid are used in the UI to get a "blank" data item
    49.    // -> i.e. to fill a data item with default values
    50.    // if this is the case, we leave the item out of the transaction
    51.    if (id != Guid.Empty)
    52.    {
    53.        this.GetContext().Add(country);
    54.    }
    55. 
    56.    return country;
    57.}

    The methods that are returning the taxons (GetDataTypeTaxon, and GetValueTypeTaxon) are working correctly.

    Regards,
    John
  4. John
    John avatar
    61 posts
    Registered:
    08 Jun 2012
    20 Jul 2011
    Link to this post
    Additional info:

    In my module code, where I install the taxonomy/taxons, I changed the IsSingleTaxon value from "false" (which is how it was set for "Colors" in the product sample) to "true". This results in a different error.

    Using the Organizer method results in an error basically saying that I can't use the Organizer.

    Using SetValue, passing the taxonomy name ("DataType" is our custom taxonomy) and selected taxon Id (Guid), results in an "Object reference not set to an instance of an object".

    I tried debug tracing and it appears to be completing the assignment in the DataExtension SetValue method, but is throwing the exception in the SitefinityAuthorizationCallHandler "Invoke" method.

    Where is that coming from? I took out the ISecuredObject stuff from my model objects, thinking that may help, but it didn't.

    As I said - I am stuck. I cannot believe how confusingly difficult it is to assign a selected taxonomy (i.e. category) to one of my custom (content-based) objects. I am using Josh Morales' Intra-Site example, using user controls for the admin stuff, so I'm not using the (unnecessarily complex) magic service-based approach from the Products example.

    As always, thanks for any help...

    Regards,
    John
  5. Victor Velev
    Victor Velev avatar
    1345 posts
    Registered:
    06 Jul 2015
    25 Jul 2011
    Link to this post
    Hi John,

    I will need some more time to explore the issue. I will contact you soon with results of my findings.

    Best wishes,
    Victor Velev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. John
    John avatar
    61 posts
    Registered:
    08 Jun 2012
    25 Jul 2011
    Link to this post
    Thanks Victor.

    We have abandoned taxonomies (and content-based modules) for now. There is just not enough documentation. The examples in the SDK are OK, but not nearly enough. They give you (mostly) working code, but no explanation of what any of it does.

    For now, we are building our module using a completely custom model and localization handling.

    Regards,
    John
  7. Victor Velev
    Victor Velev avatar
    1345 posts
    Registered:
    06 Jul 2015
    25 Jul 2011
    Link to this post
    Hi John,

    Thank you for providing this update on the topic. I will be glad to help you if you have any difficulties in future or questions regarding the custom module you are building.

    All the best,
    Victor Velev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
7 posts, 0 answered