Copied to clipboard

Flag this post as spam?

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


  • AbsolutelyN 2 posts 22 karma points
    Jul 13, 2012 @ 15:05
    AbsolutelyN
    0

    Document_AfterNew - how to automatically create child nodes?

    Hi

    I need to automatically create a couple of child nodes for a specific document type when a user creates it in the umbraco content tree. I have been trying to do it like this but am having no success:

    Document.AfterNew += Document_AfterNew;

    ...

    void Document_AfterNew(object sender, NewEventArgs e) {
            Document doc = new Document(true,((CMSNode)sender).Id);

            if (doc.ContentType.Alias == "myDocType") {
                    Document.MakeNew("Page Name", DocumentType.GetByAlias("AnotherDocType"), umbraco.BusinessLogic.User.GetCurrent(), doc.Id);
            }

    }

    The event is raised ok but the Document I'm trying to parse out of the sender is coming back empty - the UniqueId is: 00000000-0000-0000-0000-000000000000 and doc.ContentType is null. The following like returns a Document but it is as though it has not yet been created.

    Document doc = new Document(true,((CMSNode)sender).Id);

    Any thoughts on how to accomplish this?

    Thanks

     

     

  • Zakhar 171 posts 397 karma points
    Jul 13, 2012 @ 15:21
    Zakhar
    1

    Hi, try this:

     

    public class MyDocTypeCreation : ApplicationBase {

        public MyDocTypeCreation() {
            Document.New += CreateSubNodes;
        }

        private static void CreateSubNodes(Document sender, umbraco.cms.businesslogic.NewEventArgs e) {

            if (sender.ContentType.Alias == "MyDocType") {
                if (!sender.HasChildren) {
                    string name = sender.Text;
                    User currentUser = User.GetCurrent();

                    Log.Add(LogTypes.Custom, sender.Id, "Creating child node for" + name);
                    Document doc = Document.MakeNew("Child name",
                                                       DocumentType.GetByAlias("ChildType"),
                                                       currentUser,
                                                       sender.Id);
                    doc.getProperty("myProp").Value = 0;
                    doc.Save();
                    // optional
                    //doc.Publish(currentUser);
                    //library.UpdateDocumentCache(doc.Id);
            }
        }
    }

  • AbsolutelyN 2 posts 22 karma points
    Jul 13, 2012 @ 15:31
    AbsolutelyN
    0

    Wonderful - works perfectly - thank you very much!

  • Proxicode 119 posts 305 karma points
    Aug 08, 2013 @ 05:30
    Proxicode
    0

    @Zakhar This looks like exactly what I am looking for. Where does this class go? Is this a complied dll added in or is possible to do something like this from inside of the Umbraco CMS?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies