Razor query to retrieve pages by document type and property value
I need to get a list of documents for a specified document type and property.
In this case the document type is “NewsArticle” and the property is “articleCategory”
I can get a query to work by hard-coding the category as in the following example
var nodes = root.Descendants("NewsArticle").Where("ArticleCategory.Contains(\"Technology\")").OrderBy("ArticleDate desc").Take(10);
What I need to do is swap the hard coded “Technology” value with a string variable
However whatever I try I can’t get it to return any results if I use the variable. This is one example of about a dozen attempts I’ve tried (I’ve also tried ‘equals’, ‘==’ amongst other variations)
var nodes = root.Descendants("NewsArticle").Where("ArticleCategory.Contains(@articleCategory)").OrderBy("ArticleDate desc").Take(10);
I’ve got a feeling this should be simple - Can anyone help? Thanks
Thanks Jason. If I use that code I get the following error:
System.Collections.Generic.IEnumerable
If it's relevant I'm running the code in a partial view that inherits from Umbraco.Web.Mvc.UmbracoTemplatePage.
I've tried adding a using system.linq statement
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using System.Linq
@{
var root = Model.Content.AncestorOrSelf();
// value from the containing view's model...
string articleCategory = Model.Content.GetPropertyValue("articleCategory").ToString(); //get the value we want to use in Where clause below
int docID = Model.Content.Id;
// following hardcoded line works for getting technology articles
// var nodes = root.Descendants("NewsArticle").Where("articleCategory.Contains(\"Technology\")").OrderBy("ArticleDate desc").Take(10);
// need the following line to use the articleCategory variable as criteria in Where clause...
var nodes = root.Descendants("NewsArticle").Where("articleCategory.Contains(@0)", articleCategory).OrderBy("ArticleDate desc").Take(10);
}
<strong>
@articleCategory
</strong>
<table class="table table-striped table-condensed table-bordered">
<tbody>
@foreach (var node in nodes)
{
if (node.Id != docID) // don't want to display current doc in list of its related articles
{
<tr>
<td>
<span class="label label-success">
@if (node.GetProperty("articleDate").HasValue())
{
@Convert.ToDateTime(node.GetPropertyValue("articleDate")).ToString("dd-MM-yy")
}
else
{
@node.CreateDate.ToString("dd-MM-yy")
}
</span>
<a href="@node.Url">@node.Name</a>
<p>
@node.GetPropertyValue("leadText")
</p>
</td>
</tr>
}
}
</tbody>
</table>
Ah ok, you are using the strongly typed model (Model.Content) but this method is for the dynamic Model (CurrentPage). So here is the complete script using strongly typed (I've also made a few other suggestions)
@inherits UmbracoTemplatePage
@{
var root = Model.Content.AncestorOrSelf();
// value from the containing view's model...
string articleCategory = Model.Content.GetPropertyValue("articleCategory").ToString(); //get the value we want to use in Where clause below
int docId = Model.Content.Id;
// following hardcoded line works for getting technology articles
// var nodes = root.Descendants("NewsArticle").Where("articleCategory.Contains(\"Technology\")").OrderBy("ArticleDate desc").Take(10);
// need the following line to use the articleCategory variable as criteria in Where clause...
var nodes = root.Descendants("NewsArticle").Where(x => x.GetPropertyValue<string>("articleCategory") == articleCategory).OrderBy("ArticleDate desc").Take(10);
}
<strong>
@articleCategory
</strong>
<table class="table table-striped table-condensed table-bordered">
<tbody>
@foreach (var node in nodes)
{
if (node.Id != docId) // don't want to display current doc in list of its related articles
{
<tr>
<td>
<span class="label label-success">
@if (node.HasValue("articleDate"))
{
@(node.GetPropertyValue<DateTime>("articleDate").ToString("dd-MM-yy"))
}
else
{
@node.CreateDate.ToString("dd-MM-yy")
}
</span>
<a href="@node.Url">@node.Name</a>
<p>
@node.GetPropertyValue("leadText")
</p>
</td>
</tr>
}
}
</tbody>
</table>
I'm looking to take this to the next level where instead of targeting a specific document type, I'm looking for any document type that has a specific property value.
My differences here are:
I haven't been able to get the above code work
Document type needs to be any type; look for any page that has a specific property
The property I'm looking for is a boolean; featured
Razor query to retrieve pages by document type and property value
I need to get a list of documents for a specified document type and property. In this case the document type is “NewsArticle” and the property is “articleCategory”
I can get a query to work by hard-coding the category as in the following example
What I need to do is swap the hard coded “Technology” value with a string variable However whatever I try I can’t get it to return any results if I use the variable. This is one example of about a dozen attempts I’ve tried (I’ve also tried ‘equals’, ‘==’ amongst other variations)
I’ve got a feeling this should be simple - Can anyone help? Thanks
Think you should be able to use this construct
ps: http://our.umbraco.org/documentation/Reference/Querying/DynamicNode/Collections
Hi Mike - Thanks for the suggestion - however I now get the compiler error:
'System.Collections.Generic.IEnumerable
I think your issue is only the @ before the variable name. Try this:
Thanks Jason. If I use that code I get the following error:
System.Collections.Generic.IEnumerable
If it's relevant I'm running the code in a partial view that inherits from Umbraco.Web.Mvc.UmbracoTemplatePage. I've tried adding a using system.linq statement
Could you please post your entire cshtml file?
Hi Jeavon. Code pasted below, thanks...
Ah ok, you are using the strongly typed model (Model.Content) but this method is for the dynamic Model (CurrentPage). So here is the complete script using strongly typed (I've also made a few other suggestions)
Brilliant - that's done the trick. Thanks for your help!
No worries, I hope it makes sense?
Hi Jeavon
But this query doesnt work if multiple values are sent
for ex :
var searcheventtype = "Branding, Technology";
var eventsItems = Model.Descendants("EventsItem").Where("evDateStart >= DateTime.Now && evType.Contains(@0)&& Visible",searcheventtype ).OrderBy("evDateStart desc");
The above query returns only the events which has both . but ideally , all events with either of those eventTypes should be returned
Hi Arulkumar,
I looks like you are using DynamicNode with a legacy Razor Macro rather than MVC templating that this snippet was created for?
However, in DynamicNode there was a method called ContainsAny that might suit your need.
e.g.
Jeavon
Thanks Jeavon
Hey guys,
I'm looking to take this to the next level where instead of targeting a specific document type, I'm looking for any document type that has a specific property value.
My differences here are:
So for starters I have this
is working on a reply...