Copied to clipboard

Flag this post as spam?

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


  • dillorscroft 198 posts 192 karma points
    Apr 20, 2010 @ 14:40
    dillorscroft
    0

    How do you get the Id of a newly copied document in the API

    Hi,

    Using the API I'm copying a document underneath the same parent folder as the original i.e. becoming a sibling.

    I'd like to immediately publish the new document but I can't see how to get a reference to it.  Document.Copy does not return the Id as I would have expected.

    Any ideas how I can get either the new document or it's ID?

     Document doc = new Document(1234);

     doc.Copy(doc.Parent.Id,doc.User);

    Thanks

    DC

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Apr 20, 2010 @ 14:59
    Lee Kelleher
    0

    Hi Matt,

    Few of ideas...

    • If you set the "RelateToOrignal" flag to true, (of the Document.Copy method), then you could get the relation by alias type "relateDocumentOnCopy"?
    • Try "PublishWithChildrenWithResult" method on the parent Document?
    • Loop through the Document's Children and publish anything that is unpublished (and/or created within the last minute?)

    Let us know what you end up doing.

    Cheers, Lee.

  • dillorscroft 198 posts 192 karma points
    Apr 20, 2010 @ 15:17
    dillorscroft
    0

    I've not used the relate to original functionality much so I'll take a quick look at the videos on umbraco.tv

    I'm not keen on the third option as it's too risky on highly trafficked site but will look at it....

    Thanks

    DC

  • dillorscroft 198 posts 192 karma points
    Apr 20, 2010 @ 15:45
    dillorscroft
    1

    Lee,

    Thanks for the idea.  Seems to work fine using relations.  Basically after copying I loop through the pages relations and publish them then delete the relation.  Obviously Im assuming there will only ever be one type of relation for this document type but in this situation that's fine.

    Thanks again.

    //Everyone forgets to put the namespaces in code snippetts!

    using umbraco.cms.businesslogic.relation;



    Document doc = new Document(1234);

    doc.Copy(doc.Parent.Id,doc.User,true);

                foreach (Relation r in Relation.GetRelationsAsList(doc.Id))

                {

                    Document newDoc = new Document(r.Child.Id);

                    newDoc.Publish(newDoc.User);

                    // Refresh runtime cache

                    umbraco.library.UpdateDocumentCache(newDoc.Id);

    //Delete the relation

                    r.Delete();

                }

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Apr 20, 2010 @ 15:58
    Hendy Racher
    1

    Hi Matt, as Lee mentioned, the default RelationType alias for document copy is "relateDocumentOnCopy", so you can use:

    foreach (Relation r in Relation.GetRelations(doc.Id, RelationType.GetByAlias("relateDocumentOnCopy"))
    { ... }
  • dillorscroft 198 posts 192 karma points
    Apr 20, 2010 @ 16:02
    dillorscroft
    0

    Thanks Hendy.  That will certainly clean up the code a little bit.

    Thanks

    DC

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Apr 20, 2010 @ 16:55
    Lee Kelleher
    0

    Excellent, glad the Relations API worked out... I'll make a copy of your code snippet for future reference!

    Cheers, Lee.

  • nicodemus13 1 post 21 karma points
    Mar 21, 2011 @ 16:35
    nicodemus13
    0

    Is there no proper answer to this?  The solutions above are hacks, this seems really bizarre...

    thanks.

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

    I believe in the newer versions the Copy method returns a Document object of the newly copied document, ex:

    Document newDoc = sourceDoc.Copy(targetNodeId, umbraco.BusinessLogic.User.GetCurrent());
Please Sign in or register to post replies

Write your reply to:

Draft