Copied to clipboard

Flag this post as spam?

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


  • NiQ 24 posts 45 karma points
    Nov 21, 2011 @ 15:45
    NiQ
    0

    Create new document by code makes hundreds of new documents!

    Oh my... I have no idea what just happend but when I use this code:

    public BlogPostEventHandler()
    {
        Document.New += DocumentNew;
        Document.BeforePublish += DocumentBeforePublish;
    }

     

    private static void DocumentBeforePublish(Document sender, umbraco.cms.businesslogic.PublishEventArgs e)
    {
        if (sender.ContentType.Alias == "BlogPost")
      {
        DocumentType dt = DocumentType.GetByAlias("BlogPost");
        User author = sender.User;
        Document doc = Document.MakeNew("New Name", dt, author, 3640);
        doc.getProperty("title").Value = sender.getProperty("title").Value;
        doc.getProperty("date").Value = sender.getProperty("date").Value;
        doc.getProperty("topImage").Value = sender.getProperty("topImage").Value;
        doc.getProperty("mainBody").Value = sender.getProperty("mainBody").Value;
        doc.Publish(author);
        umbraco.library.UpdateDocumentCache(doc.Id);
        umbraco.library.RefreshContent();
      }
    }

    The page never refreshes after i press "Save and publish", it just loads forever and when I stop the loading and refresh the document tree there have been HUNDREDS of new documents saved into the folder id 3640!

    What am I missing out here?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Nov 21, 2011 @ 16:04
    Tom Fulton
    0

    Hi,

    Most likely you are getting an infinite loop, since when you create the New document, the BeforePublish event fires for the newly created/published document, so on and so forth...

    The best way to work around this is probably to check for the existence of the document you want to create before you create it.  That way it should only run the first time.

    Hope this helps,
    Tom

     

  • NiQ 24 posts 45 karma points
    Nov 21, 2011 @ 16:26
    NiQ
    0

    Ah I see. Thanks.

    Any idea how I in some easy way can check if the newly created document exists? I dont really know wich method to use for this..

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Nov 21, 2011 @ 16:31
    Tom Fulton
    0

    You could try something like this, checking to see if the target parent document already has a child with the name you want to create:

                Document parentDoc = new Document(3640);
                var children = parentDoc.Children;
                if (children.Where(d => d.Text == "New Name").Count() == 0)
                {
                    // The "New Name" doc does not exist under the parent, create and publish your document here
                }

    -Tom

  • Richard Soeteman 4035 posts 12842 karma points MVP
    Nov 21, 2011 @ 16:39
    Richard Soeteman
    1

    Question,

    Why would you create a new Blogpost document  underneath a Blogpost document. I would assume you create a comment Document. Then the publish will not pass the check if(sender.ContentType.Alias=="BlogPost") and the loop is ended.

    Hope this makes sense.

    Cheers,

    Richard

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Nov 21, 2011 @ 16:41
    Tom Fulton
    0

    Ahh I missed that, you are right :)  That would fix everything..

  • NiQ 24 posts 45 karma points
    Nov 21, 2011 @ 16:51
    NiQ
    0

    hehe well.. I want to create another BlogPost becuase I need to copy the post into two different folders. But the "Blogpost" document should be created under a "BlogList" document.

            if(sender.ContentType.Alias=="BlogPost")

    Doesn't this code check wich document type I am creating? When I create a "BlogPost" I create it in a "BlogList" document. Am I doing something wrong in the code?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Nov 21, 2011 @ 16:56
    Tom Fulton
    0

    In that case you'll need some code to determine whether or not the target document has already been created.  I think Richard was suggesting you could check the Content Type of the sender but not if it's the same as the "target".

    So you can use the code above to check by comparing the name, or you could also try checking the parent ID of the sender:

    if (sender.Parent.Id != 3640) {
    // the sender of this event isn't under 3640, go ahead and create/publish the new doc under 3640
    }

    -Tom

  • Richard Soeteman 4035 posts 12842 karma points MVP
    Nov 21, 2011 @ 16:56
    Richard Soeteman
    0

    Nope that is checking which document gets published, since you are checking in the published event handler. So you should check for BlogList instead I think?

    Cheers,

    Richard

Please Sign in or register to post replies

Write your reply to:

Draft