Copied to clipboard

Flag this post as spam?

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


  • Robert Mulder 79 posts 272 karma points c-trib
    May 21, 2014 @ 21:18
    Robert Mulder
    0

    Finding content via Api

    I'm a bit confused, using the API there are three ways (I have found anyway) to look for content in a SufraceController:

    • Using the UmbracoHelper
    • Using the ContentService
    • Using uQuery

    If I want to find a specific node I never know which one to use and why. The documentation doesn't really puts one over the other, except to say that uQuery is being replaced by the UmbracoHelper, but they seem to work completely different.

    For instance, if I want to find a particular node by it's document type, in uQuery I can say:

    umbraco.uQuery.GetNodesByType("SomeType").First()
    

    I can't seem to find a similar way to do this using the UmbracoHelper or the ContentService. In addition, the UmbracoHelper returns either a dynamic or an IPublishedContent interface implementation, which seems to lack any search function (like the Decendents() method).

    Maybe I'm going about this all wrong, but every time I want to look for something using the API, usually in a SurfaceController, I'm completely lost as to what approach to take...

    What is the difference between the three APIs and what do you do to find Content?

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 8x c-trib
    May 22, 2014 @ 15:19
    Lars-Erik Aabech
    100

    For published content, you should use UmbracoHelper.
    To find nodes by type, you can use Umbraco.TypedContentAtXPath("//SomeType").

    IPublishedContent has a lot of extensions, like Descendants().
    See https://github.com/umbraco/Umbraco4Docs/blob/master/Documentation/Reference/Querying/UmbracoHelper/index.md

  • Robert Mulder 79 posts 272 karma points c-trib
    May 22, 2014 @ 15:36
    Robert Mulder
    0

    Ah I see, I fear my knowledge of XPath is pretty limited and the page you refer to also has very little information as to how to query using XPath.

    Do you happen to know if there is any documentation 'out there' that describes how this works and what else I can do with the TypedContentAtXPath method?

    Also, I didn't realise that the methods like Decendents() are extension methods to IPublishedContent. Having these methods available answers nearly all my questions :)

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 8x c-trib
    May 22, 2014 @ 15:48
    Lars-Erik Aabech
    0

    TypedContentAtXPath searches the xml file umbraco.config under App_Data. (Actually an in-memory version, I believe)

    Maybe this will help you out:
    http://www.w3schools.com/XPath/

    The reason to use published content (only) is that you don't go to the database.
    However, you can do that by using ApplicationContext.Services.ContentService.
    Not that many querying options with content, though. There's no LINQ to SQL going on. (AFAIK)

Please Sign in or register to post replies

Write your reply to:

Draft