I have a collection of project data (see below) in a root content folder shared by multiply websites located on root level.
A website contains projects, and these projects is linked to the corresponding shared project data with an projectId string (property on both project and project data).
I'm now trying to find the best way of getting the linked project data on each project in my partial view. This is what I got so far (not working):
var contentRoot = Umbraco.TypedContentAtRoot();
var projectsFolder = contentRoot.First().Sibling("projectsFolder");
var project = projectsFolder.Descendants("projectData").Where("projectId == CurrentPage.projectId");
The above method isn't giving any results - the .Where() part is not working :(
How would you guys get the correct projectData node? Am I on the right path? Is this real life? etc.
Thanks for your answer! I actually tried that already, but it does not seem to work as expected (see error below). However the contentroot.First().Siblings() does the job, but it feels a bit like a hack :)
CS1928: 'System.Collections.Generic.IEnumerable<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'Children' and the best extension method overload 'Umbraco.Web.PublishedContentExtensions.Children(Umbraco.Core.Models.IPublishedContent, System.Func<Umbraco.Core.Models.IPublishedContent,bool>)' has some invalid arguments
Your answer got me looking at the docs again and I found that TypedContentAtXPath can be used instead, it seems to be a cleaner/better solution:
var projectsFolder = Umbraco.TypedContentAtXPath("//projectsFolder").First();
var contentRoot = Umbraco.TypedContentAtRoot();
var projectsFolder = contentRoot.Children().Where(x => x.DocumentTypeAlias == "projectsFolder").First();
var project = projectsFolder.Descendants("projectData").Where(x => x.Id == projectId);
You can use XPaths but it's a bit of a throw back from the old XSLT Umbraco days. That said the cache is still XML based so it should be speedy enough.
Oh - didn't know that! I'm trying your code but seems to get the same error. It's complaining about the contentRoot.Children ("does not contain a definition for 'Children'")?
CS1928: 'System.Collections.Generic.IEnumerable<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'Children' and the best extension method overload 'Umbraco.Core.Models.ContentExtensions.Children(Umbraco.Core.Models.IContent)' has some invalid arguments
I can't even do a foreach on @contentRoot.Children()?
if you have multiple root nodes TypedContentAtRoot will return a collection not a single item, so that might be why it doesn't have .Children() directly.
Model.Content.AncestorsOrSelf(1)
will return the root level for a given page (it says go to level 1 for this node)
Personally for that i would use TypedContentAtRoot, and then get the sibling from the collection, although I am not sure why, the above probably works just as well :)
Filtering on custom property with .Where()
Hi,
I have a collection of project data (see below) in a root content folder shared by multiply websites located on root level. A website contains projects, and these projects is linked to the corresponding shared project data with an projectId string (property on both project and project data).
I'm now trying to find the best way of getting the linked project data on each project in my partial view. This is what I got so far (not working):
The above method isn't giving any results - the .Where() part is not working :(
How would you guys get the correct projectData node? Am I on the right path? Is this real life? etc.
/Mathias
I haven't looked at what you are trying to do in detail - but I think your "where" should be more like
Thanks a million, Gordon!
Tried your suggestion and ended up with an error?! But eventually got it working with:
Looks like there is also a problem at line two - you're not getting the ProjectsFolder - a sibling is at the same level not below.
Try:
Hi Steve,
Thanks for your answer! I actually tried that already, but it does not seem to work as expected (see error below). However the contentroot.First().Siblings() does the job, but it feels a bit like a hack :)
Your answer got me looking at the docs again and I found that TypedContentAtXPath can be used instead, it seems to be a cleaner/better solution:
Hi,
Sorry my code should have said
You can use XPaths but it's a bit of a throw back from the old XSLT Umbraco days. That said the cache is still XML based so it should be speedy enough.
Steve
Oh - didn't know that! I'm trying your code but seems to get the same error. It's complaining about the contentRoot.Children ("does not contain a definition for 'Children'")?
I can't even do a foreach on @contentRoot.Children()?
Hi
if you have multiple root nodes TypedContentAtRoot will return a collection not a single item, so that might be why it doesn't have .Children() directly.
will return the root level for a given page (it says go to level 1 for this node)
Thanks Kevin! That makes perfectly good sense. How would you then get a root level sibling from within a given page? Would this be the best approach?
Hi
Personally for that i would use TypedContentAtRoot, and then get the sibling from the collection, although I am not sure why, the above probably works just as well :)
Doh - good spot Kevin. Right, never post code without trying it first :)
is working on a reply...