Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

sending file to blob storage by x++

(0) ShareShare
ReportReport
Posted on by 465

Hi,

so i exported a composite data entity by code, and i used     File::SendFileToUser(stream, filename); as u can see below to be able to exported file.

        #DMF
        SharedServiceUnitFileID fileId;
        DMFDefinitionGroupName definitionGroupName = "MyUniqueDefinitionGroupName";

        try
        {
            str x = "#FieldGroupName_AllFields";


            EntityName      entityName;
            DMFEntity       dmfEntity;


            select firstonly dmfEntity  order by EntityName asc where dmfEntity.TargetEntity == dataEntityViewStr(SalesOrderV3Entity);

            entityName = dmfEntity.EntityName;

            Query                query = new Query(DMFUtil::getDefaultQueryForEntity(entityName, "ExportTest"));

            DMFEntityExporter exporter = new DMFEntityExporter();
            fileId = exporter.exportToFile(entityName,
                                definitionGroupName,
                                '', //Optional: ExecutionID
                                "XML-Attribute", //Optional::SourceName
                                x, //Optional field selection
                                query.pack(), //Optional: Filtered Query
                                curExt() //Optional: DataAReaId
                                );


            if (fileId != '')
            {
                str downloadUrl = DMFDataPopulation::getAzureBlobReadUrl(str2Guid(fileId));

                System.Uri uri = new System.Uri(downloadUrl);
                str fileExt;

                if (uri != null)
                {
                    fileExt = System.IO.Path::GetExtension(uri.LocalPath);
                }
                
                Filename filename = strFmt('MyFirstExport%1',fileExt);
                System.IO.Stream stream = File::UseFileFromURL(downloadUrl);
                File::SendFileToUser(stream, filename);

            }
            else
            {
                throw error("DMF execution failed and details were written to the execution log");
            }
        }
        catch
        {
            error("error occurred while exporting");
        }

Now i want instead of downloading the file locally, to send it to blob storage, in order for a third system to read from it.

I noticed there is 2 keys and connection strings, which ones to pass?

and what to pass in GetBlockBlobReference and uploadFromFile? or is there another way?

                Filename filename = strFmt('MyFirstExport%1',fileExt);
                System.IO.Stream stream = File::UseFileFromURL(downloadUrl);
                File::SendFileToUser(stream, filename);



               Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey);

               Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);

               var blobcli = storageAccount.CreateCloudBlobClient();

               Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer cont = blobcli.GetContainerReference("from-d365");

               CloudBlockBlob cloudBlockBlob = cont.GetBlockBlobReference(fileName); // should i pass the file name i did above?
               cloudBlockBlob.UploadFromFile(); ///what to put here?

  • Sergei Minozhenko Profile Picture
    23,091 on at
    RE: sending file to blob storage by x++

    Hi D365FO_user,

    >> it seems i should not use send file and upload stream together, when i removed send file to user, the file was filled in azure.

    Looks like you need to reset stream position after uploading to user (stream.Position = 0) before you can use stream again.

    >> Btw how can i use a unique name for the file each time by x++ as here i'm putting a string name

    You can add guid as postfix to the file name docs.microsoft.com/.../xpp-run-time-functions

  • D365FO user Profile Picture
    465 on at
    RE: sending file to blob storage by x++

    it seems i should not use send file and upload stream together, when i removed send file to user, the file was filled in azure.

    Btw how can i use a unique name for the file each time by x++ as here i'm putting a string name

  • D365FO user Profile Picture
    465 on at
    RE: sending file to blob storage by x++

    it should because when i used send file to user i got values. The stream has capacity field filled so i guess it's filled but all i get is an empty file.

  • Martin Dráb Profile Picture
    231,788 Most Valuable Professional on at
    RE: sending file to blob storage by x++

    First of all, make sure that your stream contains some data.

  • D365FO user Profile Picture
    465 on at
    RE: sending file to blob storage by x++

    i passed null values but my export file in azure storage was empty.

    cloudBlockBlob.UploadFromStream(stream,null,null,null);

  • Martin Dráb Profile Picture
    231,788 Most Valuable Professional on at
    RE: sending file to blob storage by x++

    I don't understand the first question. Please start by explaining what you mean by "here".

    Regarding the error messages, try following what they're suggesting, e.g. using literal values for the parameters. If you still have a problem, show us your new code.

  • D365FO user Profile Picture
    465 on at
    RE: sending file to blob storage by x++

    1. should i pass here the name of the container i created? GetContainerReference or it doesn't matter?

    2.  I tried cloudBlockBlob.UploadFromStream(stream); but i got this:

    pastedimage1591517645078v1.png

    3. there is 2 keys and 2 connection strings, which one to pass in StorageCredentials?

  • Suggested answer
    Martin Dráb Profile Picture
    231,788 Most Valuable Professional on at
    RE: sending file to blob storage by x++

    Why do you want to use UploadFromFile()? You already have a stream, therefore simply use it as the parameter of UploadFromStream().

    The argument of GetBlockBlobReference() is a name of the blob. It doesn't have to be the same as what you used as the default file name when sending the file to users. We can't tell you what the name should be, because it depends on your functional requirements, which you didn't share with us. Simply set the name you want.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,923 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,788 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans