Copied to clipboard

Flag this post as spam?

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


  • Paul Seal 395 posts 2155 karma points MVP 2x c-trib
    Mar 02, 2019 @ 07:45
    Paul Seal
    3

    Simple Search in v8

    In v7 we could do a simple search like this:

    @{
        var searchQuery = Request.QueryString["query"];
    
        if (!string.IsNullOrEmpty(searchQuery))    
        {
            <div class="searchresults">
                <p>Your search results for <strong>@searchQuery</strong></p>
                <ul>
                    @foreach (var result in Umbraco.Search(searchQuery))
                    {
                        <li>
                            <a href="@result.Url">@result.Name</a>
                        </li>
                    }
                </ul>
            </div>
         }
    }
    

    What is the equivalent in v8 please?

    I can't find the Umbraco.Search method anymore.

  • Søren Gregersen 318 posts 1292 karma points MVP c-trib
    Mar 02, 2019 @ 08:09
    Søren Gregersen
    2

    try Umbraco.ContentQuery.Search("paul") :)

    and then @result.Content.Name and @result.Content.Url

  • Paul Seal 395 posts 2155 karma points MVP 2x c-trib
    Mar 03, 2019 @ 23:51
    Paul Seal
    0

    Thanks Soren, the code works, but the results are empty. I'm not sure why.

    I have republished the content and have rebuilt the indexes.

    Still 0 results.

  • Paul Seal 395 posts 2155 karma points MVP 2x c-trib
    Mar 04, 2019 @ 00:20
    Paul Seal
    0

    I'm trying to get the search to work in my starter kit for v8.

    If you are reading this and want to help solve this, please clone the repo and test out the search.

    https://github.com/prjseal/CodeShare-Umbraco-Starter-Kit-for-v8/

  • Ole Martin Bakke 108 posts 619 karma points
    Mar 04, 2019 @ 10:04
    Ole Martin Bakke
    103

    Hi, I got it working this way: Umbraco.ContentQuery.Search("term", string.Empty) Default of cultur = null did always return 0 resutlts.

  • Søren Gregersen 318 posts 1292 karma points MVP c-trib
    Mar 04, 2019 @ 10:17
    Søren Gregersen
    0

    Seems like a bug, more than intended behaviour :-)

  • Paul Seal 395 posts 2155 karma points MVP 2x c-trib
    Mar 05, 2019 @ 14:22
    Paul Seal
    0

    Thanks for your help Ole, it works with string.Empty.

  • Claushingebjerg 845 posts 2221 karma points
    Apr 11, 2019 @ 10:53
    Claushingebjerg
    0

    Hi Paul

    Do you have an updated snippet for a working simple search in v8?

  • Jimmy 23 posts 118 karma points
    Apr 11, 2019 @ 11:46
    Jimmy
    1

    Hi.

    This works for us in V8

    @{
         var searchQuery = Request.QueryString["query"];
         var counter = 0;
    }
    
    <ul style="padding:0px">
    @if (!string.IsNullOrWhiteSpace(searchQuery))
                        {
                            var results = Umbraco.ContentQuery.Search(searchQuery, string.Empty);
                            long resultCount = results != null && results.Any() ? results.Count() : 0;
                            <div class="searchresults">
                                @if (resultCount > 0)
                                {
    
                                        foreach (var result in results)
                                        {
    
    
    
                                              if(result.Content.ToString()=="Umbraco.Web.PublishedModels.Side") {
                                                counter++;
    
                                                <strong>@counter. <a href="@result.Content.Url">@result.Content.Name</a></strong>
                                            <br><br>
                                            }
                                        }
    
                                }
                            </div>
                        } 
    
                        @if(counter==0) {
                            <p>Vi fandt ingen resultater på dit søgeorg <strong>@searchQuery</strong></p>
                        }
    </ul>
    
  • Claushingebjerg 845 posts 2221 karma points
    Apr 11, 2019 @ 12:17
    Claushingebjerg
    0

    Thanks Jimmy :)

  • Paul Seal 395 posts 2155 karma points MVP 2x c-trib
    Apr 11, 2019 @ 13:30
    Paul Seal
    4

    This is the code I ended up using:

    @{
        var searchQuery = Request.QueryString["q"];
    }
    
    @if (!string.IsNullOrWhiteSpace(searchQuery))
    {
        var results = Umbraco.ContentQuery.Search(searchQuery, string.Empty);
        long resultCount = results != null && results.Any() ? results.Count() : 0;
        <div class="searchresults">
            <p>We found <strong>@resultCount</strong> result@(resultCount != 1 ? "s" : "") when searching for <strong>@searchQuery</strong></p>
            @if (resultCount > 0)
            {
                <ul>
                    @foreach (var result in results)
                    {
                        <li>
                            <h3><a href="@result.Content.Url">@result.Content.Name</a></h3>
                        </li>
                    }
                </ul>
            }
        </div>
    }
    

    You can see it in my starter kit here.

    https://github.com/prjseal/CodeShare-Umbraco-Starter-Kit-for-v8/blob/master/src/CSUSK.Web/Views/Search.cshtml

  • Claushingebjerg 845 posts 2221 karma points
    Apr 11, 2019 @ 13:32
    Claushingebjerg
    0

    Cool, thanks Paul

  • Bry 13 posts 93 karma points
    Apr 11, 2019 @ 13:48
    Bry
    0

    Could I possible ask, I have a number of items within a collection, would I be able to adapt the above suggested code to search a collection?

    current query

    var newsArticles = Umbraco.Content(Guid.Parse("fb4c2b24-671b-44de-8e2f-cc685a65818a"))
    .Children().OrderByDescending(x => x.CreateDate)
    .Where(x => x.IsVisible());
    
  • Elitenet 125 posts 473 karma points
    4 days ago
    Elitenet
    0

    You should be able to extend the query with LINQ, here is an example of a parent and a specific document type.

    var results = Umbraco.ContentQuery.Search(searchQuery).Where(x => x.Content.IsDocumentType("NewsArticle") || x.Content.Parent.Id == 1066);
    
  • Elitenet 125 posts 473 karma points
    4 days ago
    Elitenet
    0

    Great code, but be aware that including the searched text in the code is a possible injection attack, depending on individual setup of security in Umbraco. So I would leave out the:

    when searching for <strong>@searchQuery</strong>
    
Please Sign in or register to post replies

Write your reply to:

Draft