Copied to clipboard

Flag this post as spam?

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


  • Michael Worrall 57 posts 82 karma points
    Jul 07, 2011 @ 13:30
    Michael Worrall
    0

    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

     

  • Rich Green 2246 posts 4008 karma points
    Jul 07, 2011 @ 13:42
    Rich Green
    0

    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

  • Michael Worrall 57 posts 82 karma points
    Jul 07, 2011 @ 15:23
    Michael Worrall
    0

    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

  • Jesper Hauge 298 posts 487 karma points c-trib
    Jul 07, 2011 @ 15:51
    Jesper Hauge
    1

    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:

    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()) } }

    Regards
    Jesper Hauge 

Please Sign in or register to post replies

Write your reply to:

Draft