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?
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.
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.
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?
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.
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:
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:
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
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
Hello,
If you're using the new Razor with MVC templates you could use this extension method:
It also has a .Where(y => y != null) which means it will only return published nodes.
This code works on 6.0.4+
Jeroen
Just a quick bump because notifications didn't work. See my previous post :-).
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
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?
Each testimonial is under a testimonials container at the site root.
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?
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
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.
Isn't there a simple way to change the above to check if the picked nodes actually exist and are published?
Hi,
You probably just want to wrap a null check after the TypedContent call:
Note you can also pass in a list of IDs to TypedContent like so:
Also, I think these null checks will be handled for you automatically (maybe) if you use the PropertyValueConverters instead.
Hope that helps, Tom
Thanks Tom!
Thats awesome! Works perfectly.
is working on a reply...