Umbraco how to get document by type and name

Press Ctrl / CMD + C to copy this to your clipboard.

Copied to clipboard

Flag this post as spam?

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

Andrew 25 posts 148 karma points May 28, 2019 @ 14:51

Andrew

Get Content of a particular document type

Hi, Is there a way to get all content nodes of a particular document type? Sure it should be simple but can't seem to find anything for V8. Many thanks, Andrew

Frans de Jong 548 posts 1840 karma points MVP 4x c-trib May 28, 2019 @ 15:09

Frans de Jong

IENumerable allContentOfType = Umbraco.ContentAtXpath("//documentTypeAlias") should still work. I don't know how it performs in V8.

Bryna 74 posts 260 karma points May 28, 2019 @ 19:26

Bryna

Below also will work; however, it is leveraging LINQ as opposed to xpath, so you might read up on https://our.umbraco.com/documentation/Reference/Common-Pitfalls/. I am not sure if the statements regarding LINQ/Xpath still apply, but they are certainly worth keeping in mind.

 IEnumerable imageRotator = Model.PublishedContent().Descendants().Where(x => x.ContentType.Alias.ToLower() == "") 
Alex 18 posts 90 karma points May 30, 2019 @ 09:11

Alex

Andrew, I guess you are looking for "DescendantsOfType"? Model.DescendantsOfType("myDocType") Frans de Jong 548 posts 1840 karma points MVP 4x c-trib May 30, 2019 @ 09:13

Frans de Jong

I think descendants is more expensive than the xpath solution. It searched the entire tree and gets the items you are looking for. Xpath gets the types directly from cache. At least in v7

George Phillipson 108 posts 287 karma points May 30, 2019 @ 14:34

XPath is a good option, I'm using it on a site I'm building at the moment, as an example in my layout file I have:

int cmsHomePageId = UmbracoContext.ContentCache.GetByXPath("//home").First().Id; 

I then pass the ID to another class to return some data. If you have a quick look at https://our.umbraco.com/documentation/Reference/Common-Pitfalls/ and have a quick read of Too much LINQ - XPath is still your friend which explains XPath well

Andrew 25 posts 148 karma points Jun 04, 2019 @ 14:34

Andrew

Thanks for your help guys, using Xpath did the job. Much appreciated and thanks for the extra links, will give them a read :)