Copied to clipboard

Flag this post as spam?

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


  • Rik Hodiamont 56 posts 156 karma points
    Jan 16, 2015 @ 12:56
    Rik Hodiamont
    0

    Umbraco 7 + nodefactory

    Hi,

    I have a question. I maybe do something stupid on getting the api to work. In the past I worked with the nodefactory and umbraco 4.x in combination with the ucompontents and that worked fine.

    Now I created an new project in VS2013, add cms.dll, businesslogic.dll and umbraco.dll as a reference and made the following code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using umbraco.NodeFactory;
    
    namespace pn_autoarchive
    {
        public partial class autoarchive1 : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Node myNode = new Node(1081);
                string myValue = myNode.GetProperty("bodyText").Value;
                string nodename = myNode.Name;
                DateTime nodeCreationDate = myNode.CreateDate;
                string url = myNode.NiceUrl;
                Response.Write(myValue);
            }
        }
    }

    I put this code in a usercontrol and want to upload the usercontrol the a live umbraco 7 environment. When I build the solution I got this error:

    An exception of type 'System.IO.FileNotFoundException' occurred in pn-autoarchive.dll but was not handled in user code
    
    Additional information: Could not load file or assembly 'Umbraco.Core, Version=1.0.5211.22376, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

    On

    Node myNode = new Node(1081);

    I think this error become forward because the usercontrol couldn't find Umbraco. But when I upload the usercontrol, and put it with a macro in an umbraco page, I got the following error:

    [NullReferenceException: Object reference not set to an instance of an object.]
       pn_autoarchive.autoarchive1.Page_Load(Object sender, EventArgs e) +179
       System.Web.UI.Control.LoadRecursive() +70
       System.Web.UI.Control.LoadRecursive() +189
       System.Web.UI.Control.LoadRecursive() +189
       System.Web.UI.Control.LoadRecursive() +189
       System.Web.UI.Control.LoadRecursive() +189
       System.Web.UI.Control.LoadRecursive() +189
       System.Web.UI.Control.LoadRecursive() +189
       System.Web.UI.Control.LoadRecursive() +189
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3177
    

    I'm sure that the node ID exists.

    I'm a little bit stuck, does anyone have a hint? Thanks in advance.

    Rik

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jan 16, 2015 @ 12:59
    Jan Skovgaard
    100

    Hi Rik

    It's because that with the release of v6 all API's where changed so you should not be using the nodefactory no more. Instead you should probably have a look at the "ContentService", which you can find more information about here http://our.umbraco.org/documentation/Reference/Management-v6/Services/

    You can learn more abot services and models as well here http://our.umbraco.org/documentation/Reference/Management-v6/Services/

    I hope this helps.

    /Jan

  • Rik Hodiamont 56 posts 156 karma points
    Jan 16, 2015 @ 13:34
    Rik Hodiamont
    0

    Hi Jan,

    Thank you for your help!

    I use this code at the moment:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    
    namespace pn_autoarchive
    {
        public partial class autoarchive1 : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
                // Get the Umbraco Content Service
                var contentService = ApplicationContext.Current.Services.ContentService;
    
                //Given a `ContentService` object get Content by its Id and get a value by alias
                var content = contentService.GetById(1081);
                object value = content.GetValue("bodyText");
                string text = value as string;  
    
            }
        }
    }
    

    And I got this error:

    The given key was not present in the dictionary.

    Is the code above right and what does the error mean?

    best regards,

    Rik

  • Rik Hodiamont 56 posts 156 karma points
    Jan 16, 2015 @ 15:57
    Rik Hodiamont
    0

    I fixed the problem. Thanks for your help!

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jan 16, 2015 @ 23:00
    Jan Skovgaard
    0

    Hi Rik

    Happy to help - What was the issue from the second post with code above using the new API? Others might benefit from the solution to this as well since the error message is quite common but is displayed for different reasons it seems.

    /Jan

  • Rik Hodiamont 56 posts 156 karma points
    Jan 17, 2015 @ 16:57
    Rik Hodiamont
    1

    Hi Jan,

    I don't know for sure why the error was appearing. But I came up with this code:

     var childerencontent = contentService.GetByID(1066);
                foreach (var content in childerencontent)
                {
                    Response.Write(content.Id);
                }

    And this works :)

    Best regards,

    Rik

Please Sign in or register to post replies

Write your reply to:

Draft