I'm writing a script that displays content Nodes. However, some of them may not be published. If the node is published, I get back all the data I need:
Node activeNode = new Node(1121); @activeNode.Name @activeNode.GetProperty("tags").Value
However, when the Node is unpublished, all this data comes back blank or null. How can I get the data even if it's unpublished?
You'll want to be using Document then in this case. See Difference between Node and Document. Basically Node only gets the published items that are in the umbraco.config cache file. Document actually reflects what information is available in the database. Note that because it accesses the database, there will be a perfomance hit when your code is processed, so you may want to approach your problem in a different way.
Regardless, you can access Document properties very simular to how to access Node properties. It looks like you are attempting to do this with razor, so:
Document activeNode = new Document(1121);
// Output the name of the node: @activeNode.Text <br />
// Output the tags value: (Note: lower case on getProperty is intentional.) @activeNode.getProperty("tags").Value
Why would you display unpublished data? It is possible to use the document object as suggested but that will hit the database and really slow down performance. So you might want to rethink your solution to make sure the site performs.
@Ricahrd - it's custom code I'm putting together that I need to work in preview mode. I'm going to use Node for normal operation, but if it passes a specific ID I'll know it's preview mode and use Document to accommodate that view. It'll be used so minimally that it shouldn't have an impact.
Node data from unpublished Node?
I'm writing a script that displays content Nodes. However, some of them may not be published. If the node is published, I get back all the data I need:
Node activeNode = new Node(1121);
@activeNode.Name
@activeNode.GetProperty("tags").Value
However, when the Node is unpublished, all this data comes back blank or null. How can I get the data even if it's unpublished?
Hey Chris,
You'll want to be using Document then in this case. See Difference between Node and Document. Basically Node only gets the published items that are in the umbraco.config cache file. Document actually reflects what information is available in the database. Note that because it accesses the database, there will be a perfomance hit when your code is processed, so you may want to approach your problem in a different way.
Regardless, you can access Document properties very simular to how to access Node properties. It looks like you are attempting to do this with razor, so:
Why would you display unpublished data? It is possible to use the document object as suggested but that will hit the database and really slow down performance. So you might want to rethink your solution to make sure the site performs.
Cheers,
Richard
@Douglas - thanks, worked perfect!
@Ricahrd - it's custom code I'm putting together that I need to work in preview mode. I'm going to use Node for normal operation, but if it passes a specific ID I'll know it's preview mode and use Document to accommodate that view. It'll be used so minimally that it shouldn't have an impact.
Really appreciate the help!
is working on a reply...