Copied to clipboard

Flag this post as spam?

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


  • Nicolai Winch Kristensen 50 posts 70 karma points
    Oct 19, 2012 @ 16:03
    Nicolai Winch Kristensen
    0

    How to get ParentID when creating new Node..

    Hi 

    I have created my own datatype with a usercontrol which creates a checkboxlist based on data from an external webservice.

    Code actually works! but only on an exsiting item. When I create new items I get error.

    I have narrowed it down to what I think might be the problem. I cannot get the parentID of the new node when creating it. 

    heres part of my code a recursive method that return the property (WEbsitenodeID its a multi site solution adn I need the ID from the root of the current website)

         private int getWebsiteNodeId(Node current, int level)

         {

                //int theID = 0;

                //count down level to prevent deadlock on recursiv call

                level = level-1;

                //ask for the ancestor

                int parentID = current.Parent.Id;

                Node parent = null;          

                 parent = new Node(parentID);

                //check the ancestor type

                string ancType = parent.NodeTypeAlias;

                if (ancType == "website"  && level >  0)

                {

                    //if website node (doctype is 'website')

                    //return node ID for the Website Node

                     return parentID;  

     

                }

     

    As mentioned ocde above is tested and work. But when creating a new node it throws error on:

    int parentID = current.Parent.Id;

    My theory is that parentID isnt set yet when creating a new node (since it work when we aretalking about exsting nodes.)

    NB funny thing is when I ask for current ID for the new node it actually returns fine, its asking for parent ID that creates the problem

    A

    can anyone back my theory up and have a suggestion on how to workaround this?

     

    best regards :-)

    Nicolai 

  • Sean Mooney 131 posts 158 karma points c-trib
    Oct 19, 2012 @ 16:24
    Sean Mooney
    0

    When and where is this code being executed?

  • Nicolai Winch Kristensen 50 posts 70 karma points
    Oct 19, 2012 @ 16:49
    Nicolai Winch Kristensen
    0

    the code is being executed in an .cs file related to a usercontrol.

    ist triggered when node in backend is clicked? 

    is this what you mean?

    Best regards :-)

  • Nicolai Winch Kristensen 50 posts 70 karma points
    Oct 19, 2012 @ 17:07
    Nicolai Winch Kristensen
    0

    most of the code:

     

     public partial class VaelgKatalog : System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor

        {

            public string umbracoValue;

     

           //method that should return node with schoolid (the home node for the school)

          

            private int getWebsiteNodeId(Node current, int level)

            {

                //int theID = 0;

                //count down level to prevent deadlock on recursiv call

                level = level-1;

                //ask for the ancestor

                int parentID = current.Parent.Id;

                Node parent = null;

               

                   

                 parent = new Node(parentID);

                

                

                //check the ancestor type

                string ancType = parent.NodeTypeAlias;

                if (ancType == "website"  && level >  0)

                {

                    //if website node (doctype is 'website')

                    //return node ID for the Website Node

                  

                    return parentID;

     

                    

                      

                     

                }

                else

                {

                 

                        return getWebsiteNodeId(parent, level);

                    

                 

     

                }

     

                

                    

                

            }

     

            protected void Page_Load(object sender, EventArgs e)

            {

     

                //get the Guid for the school

                int currentID = Convert.ToInt32(Request["id"]);

                Node current =  new Node(currentID);

                int websiteId = getWebsiteNodeId(current, 20);

                Node websiteNode = new Node(websiteId);

                String theGuid = websiteNode.GetProperty("guid").Value;

     

                Guid guid = new Guid(theGuid);

                

               

     

                //get the avaialable catalogues from the service and write them out as checkboxes (doesnt check them)

                Katalog[] kataloger = new VService().GetCatalogues(guid);

                foreach (Katalog k in kataloger)

                {

                    ListItem li = new ListItem();

                    li.Text = k.Navn;

                    li.Value = k.Id.ToString();

                    CheckBoxListKatalog.Items.Add(li);

     

                    //checking the right checkboxes

                    if (umbracoValue != null)

                    {

     

                        string[] boxes = umbracoValue.Split(',');

     

                        foreach (string box in boxes)

                        {

     

                            if (li.Value == box)

                            {

     

                                li.Selected = true;

     

                            }

     

                        }

     

                    }

     

     

                }

                

                // on save(I think) storing which checkboxes that is checked

                if (Page.IsPostBack)

                {

     

                   

                foreach (ListItem item in CheckBoxListKatalog.Items)

                    {

     

                        if (item.Selected)

                        {

     

                            if (umbracoValue == null)

                            {

     

                                umbracoValue = item.Value;

     

                            }

     

                            else

                            {

     

                                umbracoValue = umbracoValue + "," + item.Value;

     

                            }

     

                        }

     

                    }

     

                }

            }

     

            public object value

            {

                get

                {   

                    //is called when click save (I think)

                    return umbracoValue;

                }

                set

                {

                    //is called when the page is loaded (should set the right checkboxes)

                    umbracoValue = value.ToString();

                }

            }

  • Sean Mooney 131 posts 158 karma points c-trib
    Oct 19, 2012 @ 17:27
    Sean Mooney
    0

    If you are working in a datatype control you might want to use the Document Api instead of the Node Api. The node Api will only give you the information that is published, the Document Api will be working with the same data as the other controls on the edit page.

  • Nicolai Winch Kristensen 50 posts 70 karma points
    Oct 22, 2012 @ 11:10
    Nicolai Winch Kristensen
    0

    Thx sean ... I didnt know that, but just read about it. makes sense now. BUT....

    how do I retrieve the docType from a document? 

     

    With node I do it likge this:  string ancType = parent.NodeTypeAlias;

    but I cannot find something similiarto NodeTypeAlias for Document?

    Best Regards :-)

    Nicolai

     

  • Sean Mooney 131 posts 158 karma points c-trib
    Oct 22, 2012 @ 20:33
    Sean Mooney
    0

    You should be able to do this:

    parent.ContentType.Alias

    Where parent is a Document

Please Sign in or register to post replies

Write your reply to:

Draft