Tuesday, March 20, 2018

X++ code to upload/Save a file in Azure location in D365


using Microsoft.Dynamics.ApplicationPlatform.Services.Instrumentation;
using Microsoft.DynamicsOnline.Infrastructure.Components.SharedServiceUnitStorage;
using Microsoft.Dynamics.AX.Framework.FileManagement;
public class AzureStorage
{
str docfiletype;
    Microsoft.Dynamics.AX.Framework.FileManagement.IDocumentStorageProvider storageProvider; 
    public void uploadfile(Filename _Filename,Filename _filePath)
    {
        guid fileGuid = newGuid();
        str fileId;
        str downloadUrl;
        System.IO.Stream    _stream;
        str fileNameAZ = strFmt('%1/%2', fileId, _fileName);//filename = Filename.Xlsx
        fileId = guid2str(fileGuid);
        _stream = File::UseFileFromURL(_filePath);//filepath = C://Temp//Filename.Xlsx
        var blobInfo = new SharedServiceUnitStorageData();
        blobInfo.Id = fileId;
        blobInfo.Category = "StorageFolder";//Folder name
        blobInfo.Name = fileNameAZ;
        blobInfo.Accessibility = Accessibility::Private;
        blobInfo.Retention = Retention::Permanent;       
        if (_stream.CanSeek)
        {
            _stream.Seek(0, System.IO.SeekOrigin::Begin);
        }
        var blobStorageService = new SharedServiceUnitStorage(SharedServiceUnitStorage::GetDefaultStorageContext());
        blobStorageService.UploadData(blobInfo, _stream);
        var uploadedBlobInfo = blobStorageService.GetData(fileId, "StorageFolder", BlobUrlPermission::Read, System.TimeSpan::FromDays(30));
//Time span for keeping the file at azure location.
        downloadUrl =uploadedBlobInfo.BlobLink;
    }
}

No comments:

Post a Comment