web
You’re offline. This is a read only version of the page.
close
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?

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    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.

  • D365FO user Profile Picture
    465 on at

    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?

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    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

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

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

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

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

  • D365FO user Profile Picture
    465 on at

    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.

  • D365FO user Profile Picture
    465 on at

    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

  • Sergei Minozhenko Profile Picture
    23,093 on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 451 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 428 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans