Copied to clipboard

Flag this post as spam?

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


  • Colin Angus Mackay 1 post 21 karma points
    Oct 06, 2010 @ 17:29
    Colin Angus Mackay
    0

    Umbraco Document.getProperty().Value throws Null Reference Exception

    I am writing a small app that links into Umbraco (a small stand-alone console application that will eventually run as a scheduled task on the server) and I'm using the Umbraco APIs (4.5.2) to make changes to the database/document.

    Here is a fragment of what I'm doing:

    IEnumerable<Document> documents = Document.GetChildrenForTree(parentDocumentId);
    foreach(Document doc in documents.Where(d => d.Published))
    {
        doc.getProperty("myData").Value = "some data"; // Exception here
        // ...other stuff here...
    }


    However I always get a NullReferenceException because there are no properties. This confuses me because I can see that there are 5 properties in the umbraco interface.

    A colleague suggested that I use a Node instead of a document, however I can't even create one as I get a NullReferenceException from the Node class constructor.

    Node myNode = new Node(-1); // NullReferenceException here


    Does anyone have any ideas?

  • Sascha Wolter 615 posts 1101 karma points
    Oct 06, 2010 @ 18:48
    Sascha Wolter
    0

    Hi Colin,

    Is the code running in the Umbraco application or in a separate application? If it is the latter then you will probably run into some trouble trying it that way, and it would actually not be a good idea to 'share' the database etc. this way. A good idea would probably be to write custom web services which your custom application can then connect to to get the data. Here is what I have been using in one Umbraco app to traverse the whole tree:

    Document[] roots = Document.GetRootDocuments();

    foreach (Document doc in roots) {

      if (doc.ContentType.Alias == "Homepage" && doc.HasChildren) {

        //add all matched documents to the docList

        AddDocumentsRecursive(doc);

      }

    }

    There are probably better ways of achieving this in code, yet that is hard to tell as I don't know what you actually want to do.

    Hope that helps,

    Sascha

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies