Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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.
try Umbraco.ContentQuery.Search("paul") :)
Umbraco.ContentQuery.Search("paul")
and then @result.Content.Name and @result.Content.Url
@result.Content.Name
@result.Content.Url
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.
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/
Hi, I got it working this way: Umbraco.ContentQuery.Search("term", string.Empty) Default of cultur = null did always return 0 resutlts.
Seems like a bug, more than intended behaviour :-)
Thanks for your help Ole, it works with string.Empty.
Hi Paul
Do you have an updated snippet for a working simple search in v8?
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>
Thanks Jimmy :)
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
Cool, thanks Paul
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());
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);
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>
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Simple Search in v8
In v7 we could do a simple search like this:
What is the equivalent in v8 please?
I can't find the Umbraco.Search method anymore.
try
Umbraco.ContentQuery.Search("paul")
:)and then
@result.Content.Name
and@result.Content.Url
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.
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/
Hi, I got it working this way: Umbraco.ContentQuery.Search("term", string.Empty) Default of cultur = null did always return 0 resutlts.
Seems like a bug, more than intended behaviour :-)
Thanks for your help Ole, it works with string.Empty.
Hi Paul
Do you have an updated snippet for a working simple search in v8?
Hi.
This works for us in V8
Thanks Jimmy :)
This is the code I ended up using:
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
Cool, thanks Paul
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
You should be able to extend the query with LINQ, here is an example of a parent and a specific document type.
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:
is working on a reply...