Copied to clipboard

Flag this post as spam?

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


  • David Armitage 503 posts 2071 karma points
    Aug 13, 2017 @ 09:57
    David Armitage
    0

    IPublishedContent vs Node - Which one to use

    Hi Guys,

    I have just updated to Umbraco 7.6.5 from an earlier versions Umbraco7. Retrieving nodes were a little slow do I have looked back how I do this and wondered if it was the right way. I have used this way for some time and think it may have changed.

    The way I have always for nodes is.

    public static List<Node> GetNodesByNodeTypeAlias(string nodeTypeAlias)
    {
        return uQuery.GetNode(-1).GetDescendantNodes().Where(x => x.NodeTypeAlias == nodeTypeAlias).ToList();
    } 
    

    From what I have read 'Node' is deprecated and replaced with 'IPublishedContent' as per this link.

    https://our.umbraco.org/forum/developers/api-questions/46631-Getting-Umbraco-Content-IPublishedContent-vs-IContent-vs-Node-vs-Document

    My question is how do I know get published content from the cache as per my example. Sometimes I use the .Where alot on my example like for instance only returning nodes of a certain doc type or only returning nodes matching .GetProperty. How do I now achieve the same with 'IPublishedContent'?

    The only example I could find is.....

    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    IPublishedContent content = umbracoHelper.TypedContent(123);
    

    Simply getting published content by Id isn't enough. I need to query using properties other than just the Id.

    Thanks in advance.

    Kind Regards

    David

  • David Armitage 503 posts 2071 karma points
    Aug 13, 2017 @ 10:20
    David Armitage
    0

    Hi,

    I just found this but again I don't think its correct. In this example the code is using both 'Node' and 'IPublishedContent' to achieve what I want.

    Very confusing....

    http://www.jondjones.com/learn-umbraco-cms/umbraco-developers-guide/umbraco-api-explained/how-to-get-data-about-another-page-in-umbraco

    var nodes = umbraco.uQuery.GetNodesByType("umbTextPage");
    if (nodes.Any())
    {
        IPublishedContent node = Umbraco.TypedContent(nodes.First().Id);
    }
    
  • Nik 1591 posts 7148 karma points MVP 6x c-trib
    Aug 13, 2017 @ 10:57
    Nik
    100

    Hi David,

    I would say you are generally better off using IPublishedcontent as opposed to Node. Dependent on what you are trying to do, I'd also consider using Examine to query the data.

    These are useful references:

    1) Common pitfalls that people do when using Umbraco, it covers things like Decendent querying. https://our.umbraco.org/documentation/reference/Common-Pitfalls/

    2) Performance Evaluation of Querying Umbraco - This was a really interesting article where Tim profiled accessing data in different ways that might be worth considering. http://skrift.io/articles/archive/testing-the-performance-of-querying-umbraco/

  • David Armitage 503 posts 2071 karma points
    Aug 14, 2017 @ 02:09
    David Armitage
    1

    Hi,

    Great reply. Thanks!

    The second link was exactly what I was looking for. It was nice to see each different method speed tested.

    Kind Regards

    David

  • Nik 1591 posts 7148 karma points MVP 6x c-trib
    Aug 14, 2017 @ 09:03
    Nik
    0

    Pleased it helped :-)

  • David Armitage 503 posts 2071 karma points
    Aug 14, 2017 @ 10:21
    David Armitage
    0

    For anyone interested....

    I used the following to do simple queries to the content cache...

    public static List<IPublishedContent> GetPublishedContentListByDocumentTypeAlias(string documentTypeAlias)
    {
        var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
        return umbracoHelper.TypedContentAtRoot().DescendantsOrSelf(documentTypeAlias).ToList();
    }
    

    You can also throw a bit of Linq in there querying the properties

    eg.

     .Where(x => x.GetPropertyValue("propertyAliasHere"))
    

    For anything more complex such as querying a number of properties then Examine works well. Examples of this in the links provided above.

    Kind Regards

    David

Please Sign in or register to post replies

Write your reply to:

Draft