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!
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.
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 }
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.
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?
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 }
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?
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:
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?
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
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..
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:
-Tom
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
Ahh I missed that, you are right :) That would fix everything..
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.
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?
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:
-Tom
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
is working on a reply...