How to cancel document creation using Document.Newing
I'm new to the Umbraco API, and am really struggling to cancel the creation of a document if one already exists within it's parent with that name (i.e. sibling). I know Umbraco automatically appends a (1), (2) etc... but I want to cancel the creation for other reasons.
I'm attempting to use the Document.Newing event for this. However it just passes in an "object" as the sender and I cannot figure out how to find out anything about the document that we are about to create. I've tried casting sender as both CMSNode and Document, and both return null.
If anyone can point me in the right direction it would be most helpful.
However Iet me attempt to explain a little more...
I've got 2 identical websites (in different languages) and I want to automatically generate the alternate language version when he user creates a document under either site. So I've created a Document.New event which duplicates the page using the Copy method, at which point i found my events in an endless loop of creating pages. So I did a simple check to see if the parent already has a page of the same name and if it does then don't duplicate the page again, and again and again....
This was all well and good however if you then create page manually with the same name as already exists then I will not end up with an alternate language version of that page - at which point I end up with a page that is not related to another - and that will cause havoc with this website.
Anyway, and i'm sorry if the above is as clear as mud...
I've figured out that the only way I can return a document object within Newing is to use HttpContent.Request to get the nodeId that is sent to the server - I'm sure there is a better way than that. Hopefully someone can point me in the correct direction.
Maybe you could use the BeforeSave event instead, if you register a handler for this event, the Umbraco API will give you a document instead of a object. Also perhaps you could attach a label property to the doctype in question named "Copied" and use this property to store info about whether or not the document has been copied. Then you could have a Document.BeforeSave event handler looking like this:
private void DocumentBeforeSave(Document sender, SaveEventArgs e)
{ // Check if doc is right type, and has not been copied
if (sender.ContentType.Alias == "newsItem" && (string)sender.getProperty("Copied").Value == "1")
{
// Get id of node to copy to - you probably have your own implementation here
var parentId = Helpers.GetSisterSiteId(); // Set Copied property
sender.getProperty("Copied").Value == "1"; // Copy Doc
sender.Copy(parentId, umbraco.helper.GetCurrentUmbracoUser())
}
}
How to cancel document creation using Document.Newing
I'm new to the Umbraco API, and am really struggling to cancel the creation of a document if one already exists within it's parent with that name (i.e. sibling). I know Umbraco automatically appends a (1), (2) etc... but I want to cancel the creation for other reasons.
I'm attempting to use the Document.Newing event for this. However it just passes in an "object" as the sender and I cannot figure out how to find out anything about the document that we are about to create. I've tried casting sender as both CMSNode and Document, and both return null.
If anyone can point me in the right direction it would be most helpful.
Thanks,
Michael
Hey Micheal,
I'm not sure I understand the logic.
Wouldn't you check to see if the doc with the name you are trying to create already exists and if it does don't try to create it?
Rich
I guess you would check in reality...
However Iet me attempt to explain a little more...
I've got 2 identical websites (in different languages) and I want to automatically generate the alternate language version when he user creates a document under either site. So I've created a Document.New event which duplicates the page using the Copy method, at which point i found my events in an endless loop of creating pages. So I did a simple check to see if the parent already has a page of the same name and if it does then don't duplicate the page again, and again and again....
This was all well and good however if you then create page manually with the same name as already exists then I will not end up with an alternate language version of that page - at which point I end up with a page that is not related to another - and that will cause havoc with this website.
Anyway, and i'm sorry if the above is as clear as mud...
I've figured out that the only way I can return a document object within Newing is to use HttpContent.Request to get the nodeId that is sent to the server - I'm sure there is a better way than that. Hopefully someone can point me in the correct direction.
Mike
Hi Michael
Maybe you could use the BeforeSave event instead, if you register a handler for this event, the Umbraco API will give you a document instead of a object. Also perhaps you could attach a label property to the doctype in question named "Copied" and use this property to store info about whether or not the document has been copied. Then you could have a Document.BeforeSave event handler looking like this:
Regards
Jesper Hauge
is working on a reply...