Copied to clipboard

Flag this post as spam?

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


  • Matt 358 posts 841 karma points
    Jan 26, 2022 @ 11:35
    Matt
    0

    Umbraco 8 search to Umbraco 9

    Hi,

    I had this search page in Umbraco 8 and tried to move it over to Umbraco 9 but doesn't seem to be working.

    @using Umbraco.Cms.Web.Common.PublishedModels;
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.Search>
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    @using System;
    @using System.Linq;
    @using Examine;
    @using Examine.Search;
    
    
    
    @{
        Layout = "Master.cshtml";
         var searchQuery = Request.QueryString["q"];
    }
    
    <div class="light-section">
        <div class="w-container">
    
            <div class="search-results-box">
                <div class="w-form">
                    <form action="@Model.Url(PublishedUrlProvider)" method="GET" id="search" class="form-3">
                        <div class="div-block-25">
                            <input type="text" class="text-field-4 w-input" placeholder="Search..." name="q" value="@searchQuery" />
                            <button class="link-block-5 w-inline-block search-text-box">Search<i class="fa fa-search"></i></button>
                        </div>
    
                    </form>
                    <div>
                        <h2 class="heading-9">Page results</h2>
                    </div>
                </div>
            </div>
            @if (!string.IsNullOrWhiteSpace(searchQuery))
            {
                var results = Umbraco.ContentQuery.Search(searchQuery, string.Empty);
                long resultCount = results != null && results.Any() ? results.Count() : 0;
                <p>We found <strong>@resultCount</strong> result@(resultCount != 1 ? "s" : "") when searching for <strong>@searchQuery</strong></p>
                if (resultCount > 0)
                {
    
                    foreach (var result in results)
                    {
    
                        <a href="@result.Content.Url(PublishedUrlProvider)" class="page-results-row w-inline-block w-clearfix" data-ix="new-interaction-7">
                            <h1 class="page-results-heading">@result.Content.Name</h1>
                            <div class="page-results-link">@(result.Content.Value<string>("metaDescription"))</div>
                            <img src="~/images/external-link-square-with-an-arrow-in-right-diagonal.png" width="27" alt="" class="image-10">
                        </a>
    
                    }
    
                }
    
            }
    

    I get the following error when trying to run it in umbraco 9

    enter image description here

    Any one able to help or advise on why this isnt working?

    Thanks

  • Dennis 75 posts 398 karma points MVP 2x
    Jan 26, 2022 @ 13:32
    Dennis
    0

    Hi there!

    Not all these services are available immediately in the view. You may need to inject them in your view with dependency injection. You'd need to add a line similar to this inside your razor view:

    @inject IPublishedUrlProvider PublishedUrlProvider
    

    For the request, you should check if you can access the request through HttpContext instead like this:

    HttpContext.Request
    

    If that doesn't work, you can inject the IHttpContextAccessor, just like the PublishedUrlProvider

    A quick google search about the content query didn't yield any results for me, I can't help you with that unfortunately. It may have been removed from the umbraco helper or the name may have changed.

  • Robert Johnston 16 posts 106 karma points
    Feb 11, 2022 @ 21:52
    Robert Johnston
    0

    As I was trying to figure this out myself I came across this:

    var results = _publishedContentQuery.Search(@searchQuery, string.Empty);

    Seems the Umbraco Helper has been removed.

  • Lorenzo 6 posts 26 karma points
    Feb 21, 2022 @ 15:45
    Lorenzo
    0

    This is the code that works for me for a simple search in Umbraco 9.

    In my "search results" template's code:

    @using Umbraco.Cms.Web.Common.PublishedModels;
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;    
    @using Umbraco.Cms.Core
    @inject IPublishedContentQuery _publishedContentQuery;
    
    @{
        Layout = "Master.cshtml";
    }
    
    @{
        var searchQuery = ViewContext.HttpContext.Request.Query["q"];
    
        <ul>
            @foreach (var result in _publishedContentQuery.Search(searchQuery))
            {
                <li>
                    <a href="@result.Content.Url()">@result.Content.Name</a>
                </li>
            }
        </ul>
    }
    

    I followed this:

    https://our.umbraco.com/documentation/reference/querying/IPublishedContentQuery/

Please Sign in or register to post replies

Write your reply to:

Draft