Copied to clipboard

Flag this post as spam?

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


  • aman 13 posts 123 karma points
    Jan 19, 2017 @ 14:55
    aman
    0

    How can I get IPublishedContent by doc type?

    Hi umbracians,

    I am storing some email templates(means samples, do not confuse with umbraco templates) in Umbraco back office. At some point I need to send email using this template(subject, body, cc, bcc, etc).

    The problem is that I cannot get this content. When I get it by Id everything works fine. But I do not want to rely on id as it changes and decided to use path and cannot do that. I get NullReferenceException when I execute following:

    contentCache.GetByRoute(true, "relative path to email template")
    

    relative path looks like "/settings/emailtemplate/registrationtemplate" Can anyone point me that is the problem as I am new to Umbraco and struggling to understand how should I do certain things.

    P.S. Before I was trying to find content be doc type alias but again I could not succeed as I could not figure out what is the relation between IContent and IPublishedContent. I could get from ContentService by doc type id but for auto generated models I need to use IPublishedContent

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    Jan 19, 2017 @ 16:55
    Nik
    0

    Hi Aman,

    Is this code executing in a place where you have access to a current Umbraco model. For example within a surface controller?

    If you were in a surface controller for example you could do the following:

    Umbraco.AssignedContent.Site().Descendents("myDocTypeAlias")
    

    This would return an IEnumerable

    If you are trying to access this in a razor view you could do the following:

    Model.Content.Site().Decendents("myDocTypeAlias")
    

    Bascially, if you have access to the Umbraco context in a request it should be fairly straight forward to get the documents by Alias.

    Word of warning about using Decendents, it can have a negative impact on performance, if you know more details about the structure of your site you might be better to use a mix of things like Decendents and Children to get the information.

    Nik

  • aman 13 posts 123 karma points
    Jan 20, 2017 @ 08:18
    aman
    0

    Hi Nik,

    I am getting this error:

    Cannot return the IPublishedContent because the UmbracoHelper was constructed with an UmbracoContext and the current request is not a front-end request."`

    It seems that it is because I am trying to get content from umbraco api controller. Is there a way to do it?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies