I have a product page, and am using a Umbraco.ContentPicker2 for selecting a brand as one of the properties. The data is stored as a UDI, i.e. umb://document/fa06e4e2418545b7afe3694e1f8e7724
Basically, I am looking to create a page with the brand, and all of the products that have the matching UDI in the product record.
Logic being something like
Select [product] where DocumentTypeAlias == "productDetail" && getproperty("productBrand") == [thisUDI]
Extremely sloppy pseudo code, but hope the idea is clear?
Hi Alex, thank you. I am a reasonably accomplished Umbraco dev. My question is more about UDI, not overall querying.
Anyone have an example of how to query a certain docType with property data stored as UDI, getting all those records?
Maybe it is just that I need to know: would I compare them string on string? Seems like a massive step back vs an Id, so there must certainly be something I am missing.
So your goal is to query umbraco for all nodes that pick other nodes by UDI? Is this a one-off report you need to generate? Or do you actually need Umbraco to be able to run this query?
Hi Mark, thank for taking a look here. I do want to query on it, yes. The example is I have a doctype (product) that uses the picker to select another doctype (brand). Umbraco stores as a UDI.
When I am on the brand landing page, I want to reverse the lookup and get all the products with that brand UDI. It would be a real-time page, so like to generate from content/ cache.
Hi Marco: I did, but not sure if the cleanest way. I am using a core project with Umbraco core, and build out View Models for each of my objects. In my ProductDetailPageViewModel, I have these 2 properties:
public BrandDetailViewModel Brand
{
get
{
if (!Content.GetProperty("productBrand").HasValue) return null;
var brand = Content.GetProperty("productBrand").DataValue;
if (!Udi.TryParse(brand.ToString(), out var udi)) return null;
var helper = new UmbracoHelper(UmbracoContext.Current);
var brandId = helper.GetIdForUdi(udi);
var brandPublishedContent = helper.TypedContent(brandId);
return new BrandDetailViewModel(brandPublishedContent);
}
}
public string BrandKey
{
get
{
if (!Content.GetProperty("productBrand").HasValue) return "";
return Brand.Content.GetKey().ToString();
}
}
This generates the BrandDetailViewModel as you can see within the Product, so I have all that data available on a given product detail page.
To do the reverse, where I get all products for a brand, here is what I then do:
public List<ProductDetailPageViewModel> GetProductDetailPageViewModelsByBrand(string brandKey)
{
var helper = new UmbracoHelper(UmbracoContext.Current);
var products = helper.TypedContentAtXPath("//productDetail");
var list = products.Select(p => new ProductDetailPageViewModel(p)).ToList();
return list.Where(productDetailPageViewModel => productDetailPageViewModel.BrandKey == brandKey).ToList();
}
The above is a Service. So it gets ALL products, then filters them down. I pass in the content key. Let me know if that makes sense for your needs. I believe I tried adding the "Where" into the linq, but it blew up, and I just moved on.
I'm relying on Models Builder for my Models, which simplifies the "foward" part.
My biggest problem is in the "reverse" part, i.e. getting all nodes in which a given Content Picker property points to a certain node.
I don't want to use XPath, since I don't know how efficient the lookup is, and was hoping to use Examine and its indexes...
I'll let you know if I come up to something usefull
Get List of Content Based on UDI search
Hi! Spent a lot of time searching for this...
I have a product page, and am using a Umbraco.ContentPicker2 for selecting a brand as one of the properties. The data is stored as a UDI, i.e. umb://document/fa06e4e2418545b7afe3694e1f8e7724
Basically, I am looking to create a page with the brand, and all of the products that have the matching UDI in the product record.
Logic being something like
Extremely sloppy pseudo code, but hope the idea is clear?
Thanks!
Hi Brad
You can use UmbracoHelper for getting nodes you need, have a look - https://our.umbraco.com/documentation/reference/querying/umbracohelper/
Be aware with using Descendants method, check out common pitfalls - https://our.umbraco.com/documentation/reference/Common-Pitfalls/
If you need products directly from the database, use content Service - https://our.umbraco.com/documentation/reference/management/services/contentservice/ This method is pretty slow, but if you need the data from the database this is the only way, I don't recommend to use SQL requests.
Try to show us the code you have and structure - we will help you with all community!
Thanks,
Alex
Hi Alex, thank you. I am a reasonably accomplished Umbraco dev. My question is more about UDI, not overall querying.
Anyone have an example of how to query a certain docType with property data stored as UDI, getting all those records?
Maybe it is just that I need to know: would I compare them string on string? Seems like a massive step back vs an Id, so there must certainly be something I am missing.
Thanks!
So your goal is to query umbraco for all nodes that pick other nodes by UDI? Is this a one-off report you need to generate? Or do you actually need Umbraco to be able to run this query?
Hi Mark, thank for taking a look here. I do want to query on it, yes. The example is I have a doctype (product) that uses the picker to select another doctype (brand). Umbraco stores as a UDI.
When I am on the brand landing page, I want to reverse the lookup and get all the products with that brand UDI. It would be a real-time page, so like to generate from content/ cache.
Hello all: checking in if anyone in Umbraco-world has experience querying on data stored as umb/UDI.
Did you ever managed to solve this?
/Marco
Hi Marco: I did, but not sure if the cleanest way. I am using a core project with Umbraco core, and build out View Models for each of my objects. In my ProductDetailPageViewModel, I have these 2 properties:
This generates the BrandDetailViewModel as you can see within the Product, so I have all that data available on a given product detail page.
To do the reverse, where I get all products for a brand, here is what I then do:
The above is a Service. So it gets ALL products, then filters them down. I pass in the content key. Let me know if that makes sense for your needs. I believe I tried adding the "Where" into the linq, but it blew up, and I just moved on.
Brad
Uhm...
I'm relying on Models Builder for my Models, which simplifies the "foward" part.
My biggest problem is in the "reverse" part, i.e. getting all nodes in which a given Content Picker property points to a certain node. I don't want to use XPath, since I don't know how efficient the lookup is, and was hoping to use Examine and its indexes...
I'll let you know if I come up to something usefull
Sure, fair enough. That is roughly what I am doing and the response is very good. The key is indexed out of the box, so it runs through quickly.
Would you mind sharing some code? :-)
is working on a reply...