Copied to clipboard

Flag this post as spam?

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


  • xleon 21 posts 43 karma points
    Sep 15, 2010 @ 13:13
    xleon
    0

    Working with Node factory in ajax webservice (find node by name)

    Hi I´m using this nice helper class to find nodes by name.

    In my case i´m using it in an ajax-json webservice.

    The problem comes out when the helper class make use of "Node.GetCurrent()" throwing a "null reference" error.

    This is my code:

    [WebMethod(Description = "Method description")]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public object GetNodeByName(string name) 
        {
            return UmbracoHelper.FindRootNode();
        }
    [WebMethod(Description = "Method description")]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public object GetNodeByName(string name) 
        {
            List<Node> list = UmbracoHelper.GetNodesByName(name, false);
            return list.Count;
        }

    I guess the scope of Node.GetCurrent() is lost in a webservice.

    Any ideas?

  • Matt Brailsford 4125 posts 22222 karma points MVP 9x c-trib
    Sep 15, 2010 @ 13:17
    Matt Brailsford
    0

    Hey Xleon,

    Do you actually need to use FindRootNode? or are you just using that for testing? as it's that method that is the issue, but your method name suggests the second codeblock is what you are really want, and should work (if you remove the first method).

    Matt

  • xleon 21 posts 43 karma points
    Sep 15, 2010 @ 13:19
    xleon
    0

    I was just testing, but none of the methods are working. 
    UmbracoHelper is using Node.GetCurrent() everywhere. And that´s throwing the error (null reference)

  • Matt Brailsford 4125 posts 22222 karma points MVP 9x c-trib
    Sep 15, 2010 @ 13:25
    Matt Brailsford
    0

    Ahh, sorry, my bad, you are right.

    You could pull back the root node yourself based on some config, or via xpath and pass that into the method GetChildNodesByName?

    Matt

  • xleon 21 posts 43 karma points
    Sep 15, 2010 @ 13:26
    xleon
    0

    Just another test:

    [WebMethod(Description = "Method description")]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public object GetCurrent()
        {
            return Node.GetCurrent().Id;
        }

    System.NullReferenceException

  • Steen Tøttrup 191 posts 291 karma points c-trib
    Sep 15, 2010 @ 13:35
    Steen Tøttrup
    0

    The current node is "calculated" based on the request, and when you're using ajax, well, the request is not the same as when you actually hit a page in Umbraco.

    But you could push the page id with the ajax request, if the node the request came from is important.

     

  • xleon 21 posts 43 karma points
    Sep 15, 2010 @ 13:58
    xleon
    1

    Solved !

    After reading this post, I added the method GetNodeFromXpath to UmbracoHelper, and this is how I get the root node:

    [WebMethod(Description = "Method description")]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public object GetCurrent()
        {
            Node rootNode = UmbracoHelper.GetNodeFromXpath("//RootNodeName");
            return rootNode.Id;
        }

    So now, UmbracoHelper would work like this:

    public Node GetRoot()
        {
            return UmbracoHelper.GetNodeFromXpath("//RootNodeName");
    } [WebMethod(Description = "Method description")] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public object GetNodeByName(string name) { Node node = UmbracoHelper.GetChildNodeByName(name, GetRoot()); return node; }

    Thanks for help!

Please Sign in or register to post replies

Write your reply to:

Draft