views:

19

answers:

1

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?

+1  A: 

The document class gets/sets information from the umbraco database. Since your running code in an out of band console application it can't find the umbraco context. Therefore throwing a null reference exception.

You need to run the code inside of the umbraco process. There is a asmx webservice that exists for third party integration. /umbraco/webservices/api/documentservice.asmx

Elijah Glover
After speaking to a number of people at the DDD Ireland conference last week, it seems that accessing Umbraco via a webservice is the best way forward for the moment. I did have a look at the source code and it really needs refactored. There are dependencies on HttpContext in "business objects" (HttpContext should only ever go in the presentation layer), which is why it failed in a console app. I believe this will be better in V5. (I don't quite know what you mean by "umbraco context" tho'.)
Colin Mackay
umbraco context is a singleton, that exists inside of an application at runtime. It's similar to httpcontext. It stores xml/storage paths/other application variables
Elijah Glover