var helper = new UmbracoHelper(UmbracoContext.Current);
var docCategory = Request["cat"];
var docCategoryString = helper.GetPreValueAsString(int.Parse(docCategory));
With Umbraco v8 is no longer possible to use this code.
Can you help me to make my code compliant with umbraco v8?
Ok...I solved.
I don't know if this is the right way but it function properly.
I've done a custom extension method to get DataType KVP
The result is a Dictionary
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Web.Composing;
namespace AFStarterKit.AFBusinessLogic
{
/// <summary>
/// Umbraco DataType Extension
/// </summary>
public static class DataTypeExtension
{
/// <summary>
/// Get KVP By DataType Name
/// </summary>
/// <param name="dataTypeName"></param>
/// <param name="keyId">0</param>
/// <returns>List of KVP</returns>
public static Dictionary<int, string> GetKVPByDataTypeName(string dataTypeName, int keyId = 0)
{
return DataTypeListContent(Current.Services.DataTypeService.GetDataType(dataTypeName), keyId);
}
/// <summary>
/// Get KVP By DataType Id
/// </summary>
/// <param name="dataTypeId"></param>
/// <param name="keyId">0</param>
/// <returns>List of KVP</returns>
public static Dictionary<int, string> GetKVPByDataTypeId(int dataTypeId, int keyId = 0)
{
return DataTypeListContent(Current.Services.DataTypeService.GetDataType(dataTypeId), keyId);
}
/// <summary>
/// DataType List Content
/// </summary>
/// <param name="dataTypeList"></param>
/// <param name="keyId"></param>
/// <returns>List of KVP</returns>
private static Dictionary<int, string> DataTypeListContent(IDataType dataTypeList, int keyId)
{
Dictionary<int, string> dataTypeValues = new Dictionary<int, string>();
if (dataTypeList != null)
{
var kvpList = (ValueListConfiguration)dataTypeList.Configuration;
if (kvpList != null && kvpList.Items != null)
{
foreach (var kvp in kvpList.Items)
{
dataTypeValues.Add(kvp.Id, kvp.Value);
}
}
if (keyId > 0)
{
return dataTypeValues.Where(kvp => kvp.Key == keyId).ToDictionary(p => p.Key, p => p.Value);
}
return dataTypeValues;
}
return null;
}
}
}
So...I now can retrieve the KVP filtered by category/ies
// DataType Name
string dataTypeName = "ChkbList-Categories";
// Request param
int keyId = (!string.IsNullOrEmpty(Request["cat"])) ? int.Parse(Request["cat"].ToString()) : 0;
// Get KVP By DataType Name (filtered)
var dataTypeKVP = AFStarterKit.AFBusinessLogic.DataTypeExtension.GetKVPByDataTypeName(dataTypeName, keyId);
Now I will work to simplify the code (if possible)...but in the meantime, I hope this can help those with the same need
I was looking at a similar thing, but then discovered that the id's exposed there aren't unique, and are only used by the datatypeeditor. (just a sequential 1, 2, 3 for prevalues added... delete the second and add another and you end up with ids 1,3,4)
I guess they are unique to a datatype so you can use like you have it.. there is also an associated database cost, as these lookups will hit the DB everytime, cacheing is an option...
Though I still don't quite get where you are getting param["cat"] from as an id.. how are you retrieving and storing an id.. when as far as umbraco is concerned you should have a checkbox with value="type 1" id="type 1" ???
Also even though you might have those ids in the datatype config.. if you edit a value on the datatype, any nodes that have the old value stored won't reflect the update as it stores the value and not the id..
eg if Type 1 needed to now be Option 1, you'd have to change the datatype and then reselect "option 1" on all the nodes that were "type 1" as the param would be unset as "Type 1" no longer exists, even though both would be the same id from the datatype config :-(
Hope this helps.
Just had another thought... you could go examine and add a computed field to store the id.. then no db lookups?
First of all, I want to thank you for giving me support about u8
I know that it is not clear because you don't know all the contest.
I try to explain it quickly (sorry my not perfect english) :-)
In the website we found a Data Type for categories named "dtCategories" (of type Umbraco.CheckboxList) which have these values:
Type 1
Type 2
Type 3
etc...
We found also a Document Type ("doc") that have a property called "docCat" of type "dtCategories" (each "doc" can be associated to one or more categories)
The template have a column with the list of categories that links to a page that will show a doc list filtered by category id
GetPrevalueAsString alternative in Umbraco v8
Hi, I'm trying to get PreValue string by Id.
With Umbraco v7 I used this code:
With Umbraco v8 is no longer possible to use this code.
Can you help me to make my code compliant with umbraco v8?
Thank you
PreValues are gone in u8 :-(
https://github.com/umbraco/Umbraco-CMS/issues/4888
So you should find that the value stored against the field is just the value and not a key to look up the value.
https://our.umbraco.com/forum/umbraco-8/96369-v8-prevalues-from-a-specific-data-type#comment-313113
Yes, I know that PreValue are gone in u8, but this is my scenario.
I receive a param (cat) that is the "Id" of the category that I must use to filter search.
If I look the field values of the nodes (in the Examine Internal Index), I see
where "docCategory" is an Umbraco.CheckBoxList
So...I'm trying to find an easy way to get the name of category using the Id received as a param
But in u8 there is no Id stored? So not sure where you are getting the Id from in your cat param... as there is no Id as such?
Model.GetPropertyValue("docCategory") will give you "Type 1" not a prevalueId as in Umb7...
Ok...I solved. I don't know if this is the right way but it function properly.
I've done a custom extension method to get DataType KVP The result is a Dictionary
So...I now can retrieve the KVP filtered by category/ies
Now I will work to simplify the code (if possible)...but in the meantime, I hope this can help those with the same need
I was looking at a similar thing, but then discovered that the id's exposed there aren't unique, and are only used by the datatypeeditor. (just a sequential 1, 2, 3 for prevalues added... delete the second and add another and you end up with ids 1,3,4)
I guess they are unique to a datatype so you can use like you have it.. there is also an associated database cost, as these lookups will hit the DB everytime, cacheing is an option...
Though I still don't quite get where you are getting param["cat"] from as an id.. how are you retrieving and storing an id.. when as far as umbraco is concerned you should have a checkbox with value="type 1" id="type 1" ???
Also even though you might have those ids in the datatype config.. if you edit a value on the datatype, any nodes that have the old value stored won't reflect the update as it stores the value and not the id..
eg if Type 1 needed to now be Option 1, you'd have to change the datatype and then reselect "option 1" on all the nodes that were "type 1" as the param would be unset as "Type 1" no longer exists, even though both would be the same id from the datatype config :-(
Hope this helps.
Just had another thought... you could go examine and add a computed field to store the id.. then no db lookups?
https://our.umbraco.com/documentation/reference/searching/examine/examine-events
First of all, I want to thank you for giving me support about u8
I know that it is not clear because you don't know all the contest.
I try to explain it quickly (sorry my not perfect english) :-)
In the website we found a Data Type for categories named "dtCategories" (of type Umbraco.CheckboxList) which have these values:
We found also a Document Type ("doc") that have a property called "docCat" of type "dtCategories" (each "doc" can be associated to one or more categories)
The template have a column with the list of categories that links to a page that will show a doc list filtered by category id
So I need a list of links like this:
...where the value of "cat" param must be the Id of category.
To retrieve the Id of each category, after many attempts, I created a custom method to get all categories from data type name.
This method return a Dictionary(int, string) that I can use for the list of categories and also to filter docs by category (using the keyId value).
As I said...I don't think this is the best solution but for now it solves the problem ;-)
no worries..
I think what u8 expects now would be...
as that key/value pairing is gone from the IPublishedContent value respresentation of radiobtn/checklist/dropdown
No...because the name of the categories could be very long and very variables and this could create problems when I have to manage the "cat" parameter
So, I think that this is actually the best solution
However...I am looking for a more performing alternative...so...every suggestion is welcome ;-)
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.