Copied to clipboard

Flag this post as spam?

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


  • umbracocool 108 posts 197 karma points
    Jun 30, 2013 @ 03:43
    umbracocool
    0

    How to check if a node is published in Razor?

    Hey guys.

    How to check if a node is published by WHERE()?

    I have this:

    @Model.NodeById(@Model.Id)._productsTree.Count();

    But using "Count()", gives me "2", still unpublished. It should not throw 2 if unpublished. Should launch "0".

    Product 5 and Product 2, are unpublished.

     

    How do I?

    Thanks!

    The image multi node tree picker:


  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jun 30, 2013 @ 05:33
    Tom Fulton
    0

    Hi,

    You shouldn't need to do any checks for the published state, as the API you're using only queries published content - if it's unpublished it (shouldn't) be shown using the NodeFactory/Razor.  I suspect you might have another issue.  Can you share more about your structure? What if you loop through the _productsTree property, do you see what you'd expect?

    -Tom

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 30, 2013 @ 12:23
    Jeavon Leopold
    0

    Hi, I think maybe I can see the problem, MNTP does allow the selection of unpublished nodes and I suspect that you are storing MNTP as Xml so actually you are iterating through the Xml which will also contain the unpublished nodes. Take a look at the Razor samples on the MNTP documentation page as they deal vwith ignoring the unpublished nodes.

    Jeavon

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jul 01, 2013 @ 12:46
    Jeroen Breuer
    2

    Hello,

    If you're using the new Razor with MVC templates you could use this extension method:

    /// <summary>
            /// Return the nodes selected with MNTP (xml only) as IPublishedContent.
            /// </summary>
            /// <param name="node"></param>
            /// <param name="propertyName"></param>
            /// <returns></returns>
            public static IEnumerable<IPublishedContent> GetMntpNodes(this IPublishedContent node, string propertyName)
            {
                var xml = node.GetPropertyValue<RawXmlString>(propertyName);
    
                if (xml != null)
                {
                    var xmlData = xml.Value;
    
                    if (!string.IsNullOrEmpty(xmlData))
                    {
                        var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                        return umbracoHelper.TypedContent(XElement.Parse(xmlData).Descendants("nodeId").Select(x => (x.Value))).Where(y => y != null);
                    }
                }
    
                return Enumerable.Empty<IPublishedContent>();
            }

    It also has a .Where(y => y != null) which means it will only return published nodes.

    This code works on 6.0.4+

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jul 02, 2013 @ 13:54
    Jeroen Breuer
    0

    Just a quick bump because notifications didn't work. See my previous post :-).

  • Iulia Maria Jensen 40 posts 71 karma points
    May 21, 2014 @ 11:20
    Iulia Maria Jensen
    0

    Hi,

    Actually in Umbraco 7 I am experiencing something similar. 

    I have the following line:

    IPublishedContent linksSpotBoxes = @Umbraco.Content(@CurrentPage.AncestorOrSelf(1).shortcutsLinkBoksPicker);

    If the box picked via the picker is unpublished, I get a YSOD.

    Any suggestions?

    /Iulia

  • Craig100 1136 posts 2523 karma points c-trib
    Sep 06, 2014 @ 18:27
    Craig100
    0

    In V7.1.6 I'm trying to output testimonials randomly but only if they're published. The following code pulls them all out, whether they're published or not. How can it be changed to only count published ones?

    var rootNode = Umbraco.TypedContentAtRoot();
    
    if(rootNode.DescendantsOrSelf("Testimonial").Any()) {
    
        int max = rootNode.DescendantsOrSelf("Testimonial").Count(); 
    

    Each testimonial is under a testimonials container at the site root.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Sep 09, 2014 @ 02:37
    Tom Fulton
    0

    Hi Craig - did you figure this out? I can't think of why that would happen - the UmbracoHelper should only query published content. Are the offending nodes in your umbraco.config?

  • Craig100 1136 posts 2523 karma points c-trib
    Sep 09, 2014 @ 11:49
    Craig100
    0

    Hi Tom,

    Further investigation just now shows you are correct and it is responding to published content only. I had an issue with my random number generator and foreach loop which I've just now fixed. This had masked the publishing control.

    Thanks

  • Claushingebjerg 939 posts 2574 karma points
    Nov 10, 2014 @ 14:27
    Claushingebjerg
    0

    Any simple way to do this?

    I have the following in an archetype, and my editors keep deleting and unpublishing picked nodes, which crashes the site.

    var sektionListValue = boxItem.GetPropertyValue<string>("sektioner");      
    var sektionList = sektionListValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
    
    foreach (var sektion in sektionList)
    {
        var sektionItem = Umbraco.TypedContent(sektion); 
        <a href="@sektionItem.Url">@sektionItem.Name </a><br/>
        }


    Isn't there a simple way to change the above to check if the picked nodes actually exist and are published?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Nov 10, 2014 @ 18:13
    Tom Fulton
    1

    Hi,

    You probably just want to wrap a null check after the TypedContent call:

    foreach (var sektion in sektionList)
    {
        var sektionItem = Umbraco.TypedContent(sektion); 
        if (sektionItem == null) {
            continue;
        }
        <a href="@sektionItem.Url">@sektionItem.Name </a><br/>
    }
    

    Note you can also pass in a list of IDs to TypedContent like so:

    foreach (var sektionItem in Umbraco.TypedContent(sektionList).Where(n => n != null))
    {
        <a href="@sektionItem.Url">@sektionItem.Name </a><br/>
    }
    

    Also, I think these null checks will be handled for you automatically (maybe) if you use the PropertyValueConverters instead.

    Hope that helps, Tom

  • Claushingebjerg 939 posts 2574 karma points
    Nov 11, 2014 @ 08:40
    Claushingebjerg
    0

    Thanks Tom!

    Thats awesome! Works perfectly.

Please Sign in or register to post replies

Write your reply to:

Draft