none
Copy list item attachment to Document library

    Question

  • Hello Techies,

    I have a requirement to copy list item attachment to document library either through workflow or event receiver.

    Requirement :

    Custom List "A" and Document Library "B" and some workflows(designer workflows) run on item adding(when a user add attachment) on custom list and in that process when the status gets changed in list "A" the attachment should get copied migrated to Document library.

    Note : user will upload only one attachment to list item and only that attachment should be copied / migrated to document library.

     Please help me and let me know how to go about it.
    Either by  workflow or event receiver.


    • Edited by Sachchin Friday, August 19, 2016 12:37 PM change
    Friday, August 19, 2016 12:29 PM

Answers

All replies

  • Hi.

    When you attach a file to a list item, you get an URL like this:

    https://zsis386.sharepoint.com/Lists/l1/Attachments/2/new%201.txt

    where "2" is the id of your listitem.

    To get the file name you can use REST call like this...

    _api/lists/getByTitle('MyList')/items(2)?$select=AttachmentFiles,Title&$expand=AttachmentFiles

    Then... You can create a Workflow that perform a rest call using the CopyTo method, passing as parameters the right server relative URL.

    CopyTo method

    Copies the file to the destination URL.

    Endpoint

    /copyto(strNewUrlbOverWrite)

    Parameters

    strNewUrl

    Type: String
    The absolute URL or server relative URL of the destination file path to copy to.

    bOverWrite

    Type: Boolean
    true to overwrite a file with the same name in the same location; otherwise false.

    HTTP method

    POST

    Response

    None


      Request example

    executor.executeAsync({
      url: "<app web url>/_api/SP.AppContextSite(@target)/web
        /getfilebyserverrelativeurl('/Shared Documents/filename.docx')
        /copyto(strnewurl='/foldername/filename.docx',boverwrite=false)
        ?@target='<host web url>'",
      method: "POST",
      success: successHandler,
      error: errorHandler
    });


    Regards,
    Bubu
    http://zsvipullo.blogspot.it

    Please mark my answer if it helped you, I would greatly appreciate it.

    Friday, August 19, 2016 12:42 PM
  • Hello Sir,
    I tried your solution, but in workflow history it shows Bad Request.

    screenshot of the SPD workflow

    Where /sites/developer/testDoc/ is the destination library where i need to migrate the attachments.
    Please let me know where am I going wrong.



    • Edited by Sachchin Sunday, August 21, 2016 9:39 AM
    Sunday, August 21, 2016 9:31 AM
  • Hi Sachchin,

    To copy the list item attachments to document library, I recommend to create event receiver for achieving this goal.

    We can use ItemAttachmentAdded event to trigger after adding an attachment to a list item, then copy the attachment to document library.

    About the ItemAttachmentAdded event:

    https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventreceiver.itemattachmentadded.aspx?f=255&MSPPError=-2147217396

    About how to get list item attachments:

    http://chithararantech.blogspot.sg/2012/07/how-to-read-sharepoint-list-items.html

    Best Regards,

    Victoria

    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Marked as answer by Sachchin Tuesday, August 23, 2016 3:24 PM
    Monday, August 22, 2016 2:46 AM
    Moderator
  • Hi,

    Have you ever find the answer to your issue?

    I'm running into the same problem and couldn't find any good answer yet...

    Please share your findings.

    Thanks in advance.

    Monday, March 20, 2017 8:22 PM