Copied to clipboard

Flag this post as spam?

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


  • Luciox 22 posts 115 karma points
    Mar 03, 2016 @ 12:19
    Luciox
    0

    Good way to test for Unpublished Content

    Hi, I'm looking for a coding tip. Quite a few of the errors we're getting with Umbraco (be it via the Rich Text Editor with macros or the preview page) seem to have the underlying issue that we are referring to unpublished content.

    This is often via a macro referring to the content in a content picker.

    Is there a good way to test in code for whether content has been published, where I have the content id?

    Many thanks

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Mar 03, 2016 @ 12:45
    Dan Diplo
    0

    If you can get content back as IPublishedContent then, by default, it should be published. So this would work (where 1234 is your content ID).

    bool isPublished = Umbraco.TypedContent(1234) != null;
    

    You can also reference UmbracoContext.Current.InPreviewMode to determine whether your page is being previewed or not (a source of some errors).

    Finally, if you can also use the ContentService if you are dealing with IContent. The following will check whether a node is published:

    var content = UmbracoContext.Current.Application.Services.ContentService.GetById(1234);
    bool isPublished = content.Published;
    

    You shouldn't use ContentService in the front-end, though, as it has performance implications because it goes direct to the database.

Please Sign in or register to post replies

Write your reply to:

Draft