Copied to clipboard

Flag this post as spam?

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


  • Sören Deger 733 posts 2844 karma points c-trib
    May 13, 2014 @ 16:08
    Sören Deger
    0

    How check if property is empty with contentService in Umbraco 7.1.1?

    Hi everyone,

    I get a System.NullReferenceException (Object reference not set to an instance of an object) if I will get a value from a empty property (Textbox multiple) with a user control for frontend. How can I check if a property is empty?

    This is the part of code with contentService:

    var contentService = ApplicationContext.Current.Services.ContentService;
    var myNode= contentService.GetById(1437);

    This works if the property is not empty:

    literalBox.Text = myNode.GetValue("bodyText").ToString();

    If I copy the dll from user control in web folder and call the page in frontend without republishing the content node this works great:

    if (myNode.HasProperty("bodyText") && !String.IsNullOrEmpty(myNode.GetValue("bodyText").ToString())
    {
    literalBox.Text = myNode.GetValue("bodyText").ToString();
    }

    But if I republish now the content node in umbraco and refresh the page in frontend again I get the System.NullReferenceException.

    This is the same issue everytime. Copy the dll in web folder and it works again. But if I republish the content node I get the Exception again.

    How can I solve this?

     

    Best regards

    Sören

     

  • Sören Deger 733 posts 2844 karma points c-trib
    May 13, 2014 @ 16:25
    Sören Deger
    0

    I have found the next curious thing now. If I get the exception from above and make nothing and wait about 30 minutes and refresh the page in frontend again suddenly it works again. Then if I publish the content node and refresh frontend I get the exception.......

  • Arild Lindeflaten 12 posts 31 karma points
    Oct 23, 2014 @ 12:43
    Arild Lindeflaten
    0

    Did you find a solution for this? I¨m getting System.NullReferenceException on this

    if (content.HasProperty("myProperty") && !String.IsNullOrEmpty(content.GetValue("myProperty").ToString()))

    If the property has a value, it works fine. It also worked fine with Umbraco 6.

  • Sören Deger 733 posts 2844 karma points c-trib
    Oct 23, 2014 @ 14:00
    Sören Deger
    3

    Hi Arild,

    this works fine for me in Umbraco 7.1.7. If you have an older version of Umbraco 7 you should be an update to the latest release.

    Is "myProperty" the alias of your property or the name? You must use the alias.

    This works for me in an eventhandler:

    void ContentService_Saved(IContentService sender, Umbraco.Core.Events.SaveEventArgs<IContent> e)
           {
               foreach (IContent content in e.SavedEntities)
               {
                   if (content.HasProperty("myProperty") && !String.IsNullOrWhiteSpace(content.GetValue<string>("myProperty")))
                   {
                       // do anything...
                   }
               }
           }

     

    Sören

  • Scott 95 posts 277 karma points
    May 15, 2017 @ 18:59
    Scott
    0

    Hi Sören

    I know not if you have found a solid solution after a few years or three :) for this but according to the documentation an approach would be the following:

    var contentService = ApplicationContext.Current.Services.ContentService;
    var myNode= contentService.GetById(1437);
    
    var bodyText= myNode.GetValue<string>("content");
    
    if(bodyText != null) {
        @bodyText
    }
    

    The reason you cannot do a myNode.GetValue("content").ToString() is that it can be empty, thus at runtime before it know it tries to bind to something empty, so you have to check for "null" before you return a string OR get the value as shown here.

    If anybody else has other ideas it would be great to hear some.

Please Sign in or register to post replies

Write your reply to:

Draft