Sunday, 24 December 2017

Attach File from a URL in Dynamics Operations 365

Introduction:
Suppose you have a File URL and you want to attach the file stored on that URL against a record in Dynamics Operation 365. Below snippet will help you in uploading the file to server and creating a new record in DocuRef table

Code Snippet:
    System.Net.WebClient webClient = new System.Net.WebClient();
    System.IO.Stream fileStream;
    System.Byte[] fileByteArr;
    DocuRef docuRef;  
    str fileURL = "www.myfilelocation.com/testfile.pdf"; //the URL of the file location
      
    //Download the file on client as byte array
    fileByteArr = webClient.DownloadData(fileURL);

    //convert byte array into a memory stream
    fileStream = new System.IO.MemoryStream(fileByteArr);

    //Use DocumentManagement class to attach the file and create a new record
    docuref = DocumentManagement::attachFile(tableId, recordId, companyId, docuTypeId, fileStream, fileName, mimeType, fileName);

No comments:

Post a Comment