I would like to put a full blog post on my home page by category. i.e. i want to get the last blog post with a category of 'test' for example.
I was able to display recent blog posts and I figured i could just modify slightly.
var root = Model.RootBlogNode; //Model.AncestorOrSelf(1);
var blogRoot = root.Children;
var blogArchive = root.Children.First();
var entries = blogArchive.Children.Where(x => x.Properties.Where(a=>a.PropertyTypeAlias=="categories" && a.HasValue=="Test"));
I was hoping entries above would give me all blog entries which have a category of 'Test'. I do have one entry which has a category of 'Test' I can step through code and I see the property categories.
I get the following error
CS1593: Delegate 'System.Func
I tried but no blog entries returned. No exception which is good but not the solution. categories is a property inside a collection of each entry which i feel is the big clue.
If I understand your questions correct you have a home page node, in your Umbraco installation, and at the same level you have the Articulate blog node. And then you want to display blogposts from a category on your home page. See the image below.
With this code you should get blogpost where the category is Test.
@{ var blogRoot = Umbraco.ContentAtRoot().DescendantsOrSelf("Articulate").FirstOrDefault(); var blogArchive = blogRoot.Children.First();
foreach(var blogposts in blogArchive.Children.Where("Visible").Where("categories == @0","TestCategory")){ <p>@blogposts.Name</p> }
}
The first thing I do is go the root of the Umbraco tree, and from there you need to find the blog node, and then we find the first children in our case it´st he blog archive. Then I am looping though each blogpost where the category is set to Test.
You can use to make a partial view or a partial view macro file, the code you should use is the same.
var root = Model.RootBlogNode;
//first child of type ArticulateArchive
var blogArchive = root.Children.OfTypes("ArticulateArchive").First();
//To lookup the 'last' item (assuming you mean 'latest' item)
var entries = blogArchive.Children
.OrderByDescending(x => x.UpdateDate)
//Get the latest item that has been tagged with a category of "Test" (case insensitive)
// categories are stored as CSV so we split
.First(x => x.GetPropertyValue<string>("categories").Split(',').InvariantContains("Test"));
Dennis (you can see below the exception/error) using blogArchive.Children.Where("Visible").Where("categories == @0","TestCategory")) You can see a few other variations that i tried without success
Shannon
I did get some success with (shown here) which is good. How would I get all blog entries that have a category of Test (or .net features). I noticed the First in the syntax but wasn't sure how i could change out to get all of the blog entries.
var entries = blogArchive.Children.First(x=>x.GetPropertyValue<string>("categories").Split(',').InvariantContains(".NET Features")); //works
var root = Model.RootBlogNode;
//first child of type ArticulateArchive
var blogArchive = root.Children.OfTypes("ArticulateArchive").First();
//To lookup the 'last' item (assuming you mean 'latest' item)
var entries = blogArchive.Children
//This ensures the latest is first
.OrderByDescending(x => x.UpdateDate)
//Get all latest items that has been tagged with a category of "Test" (case insensitive)
// categories are stored as CSV so we split
.Where(x => x.GetPropertyValue<string>("categories").Split(',').InvariantContains("Test"))
.ToArray();
I got object reference not set to an instance of an object on the .Where line
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 30: //Get all latest items that has been tagged with a category of "Test" (case insensitive)
Line 31: // categories are stored as CSV so we split
Line 32: .Where(x => x.GetPropertyValue<string>("categories").Split(',').InvariantContains("Test"))
Line 33: .ToArray();
Line 34:
However I was able to modify the where clause slighted so that I avoid those posts that have null value via the following which seemed to work
Articulate Blog Post on home page
I would like to put a full blog post on my home page by category. i.e. i want to get the last blog post with a category of 'test' for example.
I was able to display recent blog posts and I figured i could just modify slightly.
I was hoping entries above would give me all blog entries which have a category of 'Test'. I do have one entry which has a category of 'Test' I can step through code and I see the property categories.
I get the following error CS1593: Delegate 'System.Func
I am thinking this may just be linq issue.
thx
Hi Dave,
Can you rewrite this line:
Maybe like that :
Thanks, Alex
I tried but no blog entries returned. No exception which is good but not the solution. categories is a property inside a collection of each entry which i feel is the big clue.
Hi Dave,
If I understand your questions correct you have a home page node, in your Umbraco installation, and at the same level you have the Articulate blog node. And then you want to display blogposts from a category on your home page. See the image below.
With this code you should get blogpost where the category is Test.
The first thing I do is go the root of the Umbraco tree, and from there you need to find the blog node, and then we find the first children in our case it´st he blog archive. Then I am looping though each blogpost where the category is set to Test.
You can use to make a partial view or a partial view macro file, the code you should use is the same.
For the documentation about the .ContentAtRoot take a look here: https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/#ContentAtRoot%28%29
And here is also a strongly typed vesion available: https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/#TypedContentAtRoot%28%29
Hope this helps and don't hesitate to ask again,
/Dennis
Something like this?
I appreciate the help.
Dennis (you can see below the exception/error) using blogArchive.Children.Where("Visible").Where("categories == @0","TestCategory")) You can see a few other variations that i tried without success
Shannon I did get some success with (shown here) which is good. How would I get all blog entries that have a category of Test (or .net features). I noticed the First in the syntax but wasn't sure how i could change out to get all of the blog entries.
thanks for the support.
try something like this:
I got object reference not set to an instance of an object on the .Where line
However I was able to modify the where clause slighted so that I avoid those posts that have null value via the following which seemed to work
The ToArray was a missing piece. Thanks again!
Yes, it's because you need a null check there, if the property is empty, it will be null
is working on a reply...