Monday, 6 October 2025

Upload file to cloud/azure blob storage in D365 FO X++

 Class to upload file stream to a cloud

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
Using Microsoft.WindowsAzure.Storage;
Using Microsoft.WindowsAzure.Storage.Blob;
/// <summary>
/// Uplpoads file stream to a cloud
/// </summary>
class VKUploadToCloud
{
    str storageConnStr;
    str containerName;
 
    public static VKUploadToCloud construct(str _storageConnStr, str _containerName = '')
    {
        VKUploadToCloud uploadToCloud = new VKUploadToCloud();
 
        uploadToCloud.parmStorageConnStr(_storageConnStr);
        uploadToCloud.parmContainerName(_containerName);
 
        return uploadToCloud;
    }
 
    public str parmStorageConnStr(str _storageConnStr = storageConnStr)
    {
        storageConnStr = _storageConnStr;
 
        return storageConnStr;
    }
 
    public str parmContainerName(str _containerName = containerName)
    {
        containerName = _containerName;
 
        return containerName;
    }
 
    /// <summary>
    /// Uploads stream to a cloud
    /// </summary>
    /// <param name = "_blobName">file anme</param>
    /// <param name = "_reportMs">stream</param>
    /// <returns>URL</returns>
    public URL upload(str _blobName, System.IO.MemoryStream _reportMs)
    {
        System.Exception    ex;
        URL                    downloadLink = '';
 
        try
        {
            CloudBlobClient         cloudBlobClient;
            CloudBlobContainer      cloudBlobContainer;
            CloudStorageAccount     cloudStorageAccount;
            CloudBlockBlob          cloudBlockBlob;
 
            cloudStorageAccount     = CloudStorageAccount::Parse(storageConnStr);
            cloudBlobClient         = cloudStorageAccount.CreateCloudBlobClient();
            cloudBlobContainer      = cloudBlobClient.GetContainerReference(containerName);
            cloudBlockBlob          = cloudBlobContainer.GetBlockBlobReference(_blobName);
 
            cloudBlockBlob.UploadFromStream(_reportMs, null, null, null);
            downloadLink = cloudBlockBlob.Uri.AbsoluteUri;
        }
        catch(Exception::CLRError)
        {
            ex = ClrInterop::getLastException();
            if (ex != null)
            {
                ex = ex.get_InnerException();
                if (ex != null)
                {
                    error(ex.ToString());
                }
            }
        }
        catch(ex)
        {
            error(ex.Message);
        }
 
        return downloadLink;
    }
 
}
Usage:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
            System.Byte[]                    reportBytes = new System.Byte[0]();
            reportBytes = srsproxy.renderReportToByteArray(srsReportRunController.parmReportContract().parmReportPath(),
                parameterValueArray,
                printerSettings.fileFormat(),
                printerSettings.deviceinfo());
 
            if (reportBytes)
            {
                System.IO.MemoryStream     stream = new System.IO.MemoryStream(reportBytes);
                URL                        downloadLink;
                VKUploadToCloud            uploadToCloud = VKUploadToCloud::construct(
                        'DefaultEndpointsProtocol=https;AccountName={AccountName};AccountKey={AccountKey};EndpointSuffix=core.windows.net',
                        'invoices'
                    );
                 
                downloadLink = uploadToCloud.upload(fileName, stream);
            }

No comments:

Post a Comment

Creating new D365FO Business Event

  The name should include the noun or phrase that corresponds with theevent, followed by the BusinessEvent suffix. New business event for Cu...