Copied to clipboard

Flag this post as spam?

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


  • Owain Williams 479 posts 1410 karma points MVP 6x c-trib
    Feb 16, 2021 @ 08:22
    Owain Williams
    0

    Examine query that adds results to a list

    Hi everyone, I'm looking for some help. I've got a requirement to display a dropdown list of the types of Document Types / Pages that are available to filter by. Just now for example I have a list with

    Article
    Video
    Podcast
    

    And that works fine, it pulls back a Distinct list of types, however, I have a Document Type called Generic and on that docType I have a content picker which allows the user to select a Content Label, this should then override the Document Type and display on the dropdown list

    So instead of

    Article
    Video
    Podcast
    Generic
    

    It should show

    Article
    Video
    Podcast
    Interview
    

    If interview was the label selected on the Generic Document Type.

    Here is my code so far:

    public ActionResult GetDistinctContentTypesForTopics(string topic)
        {
    
            var searcher = ExamineManager.Instance.SearchProviderCollection["ContentHubSearcher"];
    
            ISearchCriteria searchCriteria = searcher.CreateSearchCriteria(IndexTypes.Content);
            IBooleanOperation query = searchCriteria.GroupedOr(new string[] { "pagePostTags" }, topic);
    
            var queryResults = searcher.Search(query.Compile());
            IEnumerable<string> contentLabel = queryResults.Select(x => x["contentLabel"]).Distinct().ToList();
    
            List<string> contentLabelText = new List<string>();
    
    // This gets the label if one is present, I should maybe check if it's of Type Generic too.
            if (contentLabel != null || contentLabel.Any())
            {
                foreach (var label in contentLabel)
                {
                    var labelGuid = Umbraco.TypedContent(label);
                    if (labelGuid != null)
                    {
                        if(labelGuid.HasValue("labelTitle"))
                        {
                            var labelText = labelGuid.GetPropertyValue<string>("labelTitle");
                            contentLabelText.Add(labelText);
                        }       
                    }
                }
            }
    
    
            // If nodeTypeAlias has Content Label, use that instead of nodeAlias
            var allSearchResults = queryResults.Select(x => x["nodeTypeAlias"]).Distinct().ToList();
    
            List<string> allResults = contentLabelText.Union(allSearchResults).Distinct().ToList();
    
            return PartialView("Topics/ContentTypesDropdown", allResults);
    
    
        }
    

    I tried to do a union of the two lists that I have but that doesn't work because I'm guessing I'm still pulling back the Generic Document Type.

    If anyone has some time to help me with this it would be amazing. It's Umbraco 7 project.

    Thanks, O.

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Feb 16, 2021 @ 08:30
    Nik
    0

    Hey Owain :-D

    What about if you modify the indexing process to put values in a specific field rather than searching node type alias.

    You could put a custom field called "DocumentType" and in that store the node type for your normal docs, but for the generic doc type store the tagged label instead.

    Then when you get your results back you can get the distinct values from that instead.

    Nik

  • Owain Williams 479 posts 1410 karma points MVP 6x c-trib
    Feb 16, 2021 @ 08:49
    Owain Williams
    1

    Hey, Well, funny you should say that. In my index I have:

    nodeTypeAlias: genericContent and contentLabel: 8633135048b64d8a85cda7357819f28d

    So really, I should swap out the genericContent nodeTypeAlias and convert the guid to the label?

    O.

Please Sign in or register to post replies

Write your reply to:

Draft