Copied to clipboard

Flag this post as spam?

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


  • Carole 10 posts 30 karma points
    Aug 14, 2012 @ 18:03
    Carole
    0

    How to access a custom property in a content Node from code

    Hello

    I added a property (alias = "rooms") to a document type on my umbraco website.Then I added a content node using this document type, filled the property, and I need to access it from my C# code (usercontrol for example).
    So I tried:

    Node myNode = new Node(1234);

    string myPropertyValue = myNode.GetProperty("rooms").Value;

    But It seems myNode.GetProperty("rooms") is null, even if the property is filled in the backoffice and the content node is published !

    I don't understand since I thought I should use the Node class to access to the XML cache, containing the published nodes. But the informations of this node doesn't seem up-to-date (I tried to parse the 'Properties' field of myNode to see what's inside...). I tried to reload the cache (re-publishing the home content node, recycling the application pool, etc). Nothing changed.

    By the way I'm using umbraco 3 (I can't upgrade...) but I think I'm missing something in my code, I can't see what.

    Thanks in advance for you help.

     

  • Nigel Wilson 945 posts 2077 karma points
    Aug 14, 2012 @ 22:31
    Nigel Wilson
    0

    Hi Carole

    1. Double check the alias of the "rooms" property on your document type and make sure you have it exactly the same as in your c# code - it is case sensitive so won't work if they don't match.

    2. Try republishing your entire site (right click on the top level node and select "Republish entire site".

    Cheers, Nigel

     

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Aug 14, 2012 @ 23:51
    Bo Damgaard Mortensen
    0

    Hi Carole,

    Adding to Nigels post, I think it's generally a good practice to check for null values and empty string values when accessing a property. Like this:

    if(myNode.GetProperty("rooms").Value != null && !string.IsNullOrEmpty(myNode.GetProperty("rooms").Value)) 
    {

    }

    At least that's what I'm doing. We just have to keep in mind that when working with the Document object, every .getProperty("propertyAlias").Value is a database hit, so Node should be used for reading data when possible :-)

  • Carole 10 posts 30 karma points
    Aug 21, 2012 @ 10:18
    Carole
    0

    Thanks to both of you for answering.

    To Nigel : I checked the alias (in fact I already tried rooms, Rooms, ...) and republished the entire site. Nothing changed. Even if I recycle the application pool. I still have old informations in the object and I can't figure out why. The alias "rooms" doesn't even appear in the Properties collection of the object.

    I'm not sure how the cache works in umbraco, but I noticed that when I publish the node in the backoffice, the cmsContentXml Table is updated, BUT the object's Properties collection is still up-to-date.

  • Carole 10 posts 30 karma points
    Aug 22, 2012 @ 10:26
    Carole
    0

    I finally figured out what was happening. It was a cache issue : the IIS process didn't have access to the umbraco.config file, so when I republished the site, the update couldn't be completed. If it can help someone...

    Thanks for your help.

Please Sign in or register to post replies

Write your reply to:

Draft