Copied to clipboard

Flag this post as spam?

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


  • Harv 18 posts 38 karma points
    Sep 16, 2010 @ 15:14
    Harv
    0

    Problems adding data to Umbraco from console app

    I've created a website using Umbraco 4.5 which is working fine. I have to import data into the site from an outside source so I've used a console app but I cannot get it to work.

    I just copied the code I found here:
    http://en.wikibooks.org/wiki/Umbraco/Samples_and_Articles/Creating_umbraco_pages_programmatically

     

     

     

    So the exact code I am using is as follows:

    // The documenttype that should be used, replace 10 with the id of your documenttype
    var dt = new DocumentType(1234);
            var parentNodeId = 1234;

            // The umbraco user that should create the document, 
            // 0 is the umbraco system user, and always exists
            var u = new umbraco.BusinessLogic.User(0);

            // Create the document
            Document d = Document.MakeNew("Test", dt, u, parentNodeId);

            // Add values to the generic properties of the document
            d.getProperty("MyPropertyName").Value = "11111";

            // Set the publish status of the document and there by create a new version
            //d.Publish(u); 

            // Tell the runtime environment to publish this document
            //umbraco.library.UpdateDocumentCache(d.Id);

    The document type and user are created fine, the problem is when it tries to make a new document it does not understand the document type I gave it. It throws an exception of "The given key was not present in the dictionary", and after looking through the Umbraco source code the problem lies in "public IDataType GetNewObject(Guid DataEditorId)" method in the "umbraco.cms.businesslogic.datatype.controls" class - the "_controls" dictionary object has no values.


    Firstly I'm not sure why it's doing this as it seems to have everything it needs to create the document? I know the problem has probably got something to do with running Umbraco outside of a web app but I don't know how to fix it.

    Can anyone help?

  • Richard Soeteman 4049 posts 12922 karma points MVP 2x
    Sep 16, 2010 @ 16:07
    Richard Soeteman
    1

    Hi You need a HttpContext to work with the API. So you can't call the API directly from a console App. You can use a webservice.

    Just curious did you try the free version of CMSImport ? It will save you a lot of time.

    Cheers,

    Richard

  • Harv 18 posts 38 karma points
    Sep 16, 2010 @ 16:14
    Harv
    0

    Richard thanks for the swift reply.

    Indeed I was going to use a webservice if the console app failed. But I'm now intrigued by the CMSImport as I need to import CSV data.

    Thanks for your suggestion I will look into this.

    Cheers

    H

Please Sign in or register to post replies

Write your reply to:

Draft