Copied to clipboard

Flag this post as spam?

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


  • EV 36 posts 147 karma points
    Sep 27, 2019 @ 11:19
    EV
    0

    Querying for IPublishedContent returns unpublished content as well

    Hi,

    I have a Partial View Macro with the following code:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    
    
    @{
        var selection = Model.Content.Site().FirstChild("articlesRepository").Children("article");
    
        var counter = 0;
    }
    
    
    @foreach(var item in selection){
    
            <div class="row">
                <div class="panel">
                    <div class="@(counter % 2 == 0 ? "odd-panel-heading" : "even-panel-heading")">
                        <div class="panel-heading">
                            <b>@item.GetPropertyValue("articleTitle")</b>
                        </div>
                    </div>
                    <div class="panel-body">
                        @(Umbraco.Truncate(item.GetPropertyValue<string>("articleBody"), 300))
    
                        <a href="@item.Url">Read more</a>
                    </div>
                </div>
            </div>
    
    
        counter = counter + 1;
    }
    

    And this (or more like the query above) returns the unpublished content nodes too. I tried checking with item.Published which told me that IPublishedContent doesn't have such property (true), after which I tried adding this using statement: @using Umbraco.Core.Models to try to get the Content Model so that I can use .Published, but to no avail.

    What am I doing wrong?

    Thanks in advance!

  • EV 36 posts 147 karma points
    Sep 30, 2019 @ 06:43
    EV
    0

    Anyone has an idea why can this happen?

  • EV 36 posts 147 karma points
    Oct 01, 2019 @ 06:48
    EV
    0

    Sorry for writing again, but I really can't figure this one out. I'm using the query builder for the var selection, and in the preview it gives me for which content items will be returned it's omitting the unpublished ones. However, it still returns those too.

    I have tried this too:

    var selection = Model.Content.Site().FirstChild("articlesRepository").Children()
                            .Where(x => x.IsVisible());
    

    and it's with the same outcome.

    Thank you a lot for helping!

  • Tor Langlo 189 posts 532 karma points
    Oct 01, 2019 @ 08:05
    Tor Langlo
    0

    Hi,

    Try using the IPublishedContent.IsDraft property as in

    if (!item.IsDraft)
    {
    }
    

    The IsVisible() method is different. It is tied to the presence of a "umbracoNaviHide" property in your document type, and whether you have set that property to true or false. It has nothing to do with whether the content is published or not.

    Hope this helps, -Tor

  • Steve Megson 151 posts 1022 karma points MVP c-trib
    Oct 01, 2019 @ 08:37
    Steve Megson
    100

    Possibly a stupid question, but do you have a UMB_PREVIEW cookie that has somehow not been cleared properly, leaving you trapped in preview mode? That would be consistent with what you're seeing. Preview shows all unpublished content, but the query builder won't check whether you're in preview mode and will always show just published content.

  • EV 36 posts 147 karma points
    Oct 01, 2019 @ 11:15
    EV
    0

    So, it seems like the problem was only in preview mode as you suggested! I wasn't aware about that difference between preview vs no preview mode, and soon enough realized that there's no problem.

    So to recap, there wasn't a problem, the unpublished content doesn't show when I'm not in preview mode, but indeed as you said I see all posts (even the unpublished ones) being listed when I'm in preview mode.

    Thanks a lot!

Please Sign in or register to post replies

Write your reply to:

Draft