Copied to clipboard

Flag this post as spam?

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


  • Alec Colebrook-Clark 134 posts 258 karma points
    Jun 23, 2009 @ 11:11
    Alec Colebrook-Clark
    1

    Changing a newly created Pages name.

    Hi,

     

    Sorry for the possibly confusing subject name. Basically the problem is i want to create a child node to have the same name as the parent however i havent any idea how to do this as i have exhausted what little knowledge i already have.

    my code is as follows:

     

                        DocumentType dt = DocumentType.GetByAlias("Gallery");
                        User author = User.GetUser(0);
                        int id = sender.Id;
                        string name = sender.getProperty("nodename").Value;

                        //create a document with a name, a type, an umbraco user, and the ID of the document's parent page. To create a document at the root of umbraco, use the id -1
                        Document doc = Document.MakeNew(name, dt, author, id);

                        //after creating the document, prepare it for publishing
                        doc.Publish(author);

                        //Tell umbraco to publish the document
                        umbraco.library.PublishSingleNode(doc.Id);

     

    Cheers,

    Alec

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jun 23, 2009 @ 11:28
    Simon Dingley
    1

    The property you need to set is doc.Text to set the name I believe.

  • Alec Colebrook-Clark 134 posts 258 karma points
    Jun 23, 2009 @ 11:39
    Alec Colebrook-Clark
    0

    Im struggling to pull out the parents name, its throwing a wild error now so my last effort wasnt particularly any good. However thanks for the method, It looks to be doing the job.

     

    Alec

     

    p.s On the side note, another Plymouth member? must say I'm rather shocked!

  • Alec Colebrook-Clark 134 posts 258 karma points
    Jun 23, 2009 @ 11:48
    Alec Colebrook-Clark
    0

    Its solved.

    I took what was given and stored it in a variable (after fetching the correct parent node).

     

    Thanks for the help it was bugging me for ages!

     

    Alec

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jun 23, 2009 @ 15:43
    Simon Dingley
    0

    Glad you sorted it, perhaps post up your solution in case it helps ayone else out.

    I didn't know of any other local members either until the launch of this site and the new member locator, handy to know for future.

  • Alec Colebrook-Clark 134 posts 258 karma points
    Jun 24, 2009 @ 10:21
    Alec Colebrook-Clark
    1

    For future reference my code that cracked the problem is

            void Document_AfterSave(Document sender, umbraco.cms.businesslogic.SaveEventArgs e)
            {
               
                //Create a variable
                Document currentPage = new Document(sender.Id);
               
                //Check if the contenttype is not taxonomy otherwise an endless loop will occur
                if (sender.ContentType.Alias == "Taxonomy")
                {
                    if (sender.HasChildren == false)
                    {
                       
                        //Get the document type you would like to use by its alias and the user who should be the creator of the document
                        DocumentType dt = DocumentType.GetByAlias("Gallery");
                        User author = User.GetUser(0);
                        int id = sender.Id;
                        string nodename = currentPage.Text;

                        //create a document with a name, a type, an umbraco user, and the ID of the document's parent page.
                        Document doc = Document.MakeNew(nodename + " Gallery", dt, author, id);

                        doc.Text = nodename;

                        //after creating the document, prepare it for publishing
                        doc.Publish(author);

                        //Tell umbraco to publish the document
                        umbraco.library.UpdateDocumentCache(doc.Id);
                    }
                }
            }

    The problem I had was pulling through the parents name. Turns out I hadnt included the .Alias. Hope it helps!

     

    Alec

Please Sign in or register to post replies

Write your reply to:

Draft