Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Brad Bisgard 8 posts 88 karma points
    Jan 08, 2019 @ 22:50
    Brad Bisgard
    0

    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

    Select [product] where DocumentTypeAlias == "productDetail" && getproperty("productBrand") == [thisUDI]
    

    Extremely sloppy pseudo code, but hope the idea is clear?

    Thanks!

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jan 09, 2019 @ 12:57
    Alex Skrypnyk
    0

    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

  • Brad Bisgard 8 posts 88 karma points
    Jan 09, 2019 @ 15:58
    Brad Bisgard
    0

    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!

  • Mark Bowser 273 posts 860 karma points c-trib
    Jan 09, 2019 @ 16:32
    Mark Bowser
    0

    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?

  • Brad Bisgard 8 posts 88 karma points
    Jan 09, 2019 @ 16:54
    Brad Bisgard
    0

    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.

  • Brad Bisgard 8 posts 88 karma points
    Jan 10, 2019 @ 23:34
    Brad Bisgard
    0

    Hello all: checking in if anyone in Umbraco-world has experience querying on data stored as umb/UDI.

  • Marco Lusini 176 posts 1370 karma points
    Aug 05, 2019 @ 16:51
    Marco Lusini
    0

    Did you ever managed to solve this?

    /Marco

  • Brad Bisgard 8 posts 88 karma points
    Aug 05, 2019 @ 17:25
    Brad Bisgard
    0

    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.

    Brad

  • Marco Lusini 176 posts 1370 karma points
    Aug 06, 2019 @ 09:28
    Marco Lusini
    0

    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

  • Brad Bisgard 8 posts 88 karma points
    Aug 06, 2019 @ 17:20
    Brad Bisgard
    0

    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.

  • Marco Lusini 176 posts 1370 karma points
    Aug 07, 2019 @ 06:26
    Marco Lusini
    0

    Would you mind sharing some code? :-)

Please Sign in or register to post replies

Write your reply to:

Draft