Translate

Friday, June 27, 2025

☁️ Execute D365 F&O SSRS Report with Parameters and Upload Report Output to Azure Blob Storage Using X++

In many real-world scenarios, businesses need to automatically generate SSRS reports from Dynamics 365 Finance & Operations (D365FO) and store them externally for auditing, sharing, or integration purposes. This blog walks you through how to execute an SSRS report with parameters, convert it into a PDF byte stream, and upload the output to Azure Blob Storage using X++ and the latest Azure SDK.

In this article, we'll walk through how to:

  • Execute a parameterised SSRS report using X++

  • Render the report as a PDF byte stream

  • Upload the output to Azure Blob Storage using the latest Azure SDK (Azure.Storage.Blobs)

✅ Why Use This?

Uploading reports to Azure Blob Storage allows you to:

  • Automate report delivery

  • Store large reports securely

  • Integrate with Power BI, Logic Apps, or third-party APIs

  • Replace manual downloads or email-based report distribution

✅ Steps to Achieve This

  1. Create a controller class to run the SSRS report with parameters.

  2. Create an Azure Storage Account in your Azure subscription.

  3. Create Blob containers and optional folders to organise your report files.

  4. Get the connection string from Azure to authenticate from X++.

  5. Render the SSRS report to a byte stream in PDF format.

  6. Upload the stream to Azure Blob Storage using Azure.Storage.Blobs.

🛠 Prerequisites

  1. Add references to these .NET assemblies:
    • Azure.Storage.Blobs
    • System.IO
  2. Ensure your storage account connection string and container are correctly configured.

🌐 How to Set Up Azure Storage for Report Uploads

Follow these steps in the Azure Portal to create the required storage setup:

🔹 Step 1: Log in

Visit https://portal.azure.com and sign in with your Azure credentials.

🔹 Step 2: Create Storage Account

  • Navigate to Storage accounts

  • Click + Create

  • Fill in details: Subscription, Resource Group, Name, Region

  • Choose Standard performance and Hot access tier (default)

🔹 Step 3: Create Blob Container

  • Once the storage account is created, go to it

  • Select Storage browser from the left menu

  • Click on Blob containers

  • Create a new container (e.g., reportupload)

    • Set access level to Private (recommended for security)

🔹 Step 4: (Optional) Create Folders

  • Open your container

  • Click + Add directory to create folders like ReportUpload

🔹 Step 5: Get the Connection String

  • Go to the Access keys section under Security + networking

  • Copy the Connection string of key1

    • Example format:

(DefaultEndpointsProtocol=https;AccountName=youraccount;AccountKey=yourkey;EndpointSuffix=core.windows.net)

🔄 What's Changed?

The legacy WindowsAzure.Storage package is deprecated. The new library:

  • Uses Azure.Storage.Blobs.

  • Has updated method names and authentication mechanisms.

  • It is available via NuGet: Azure.Storage.Blobs

💻 X++ Code with Latest SDK

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Specialized;
using System.IO;
 
public class ExampleUploadReportToBlobStorage
{
    public static void main(Args _args)
    {
        ExampleUploadReportToBlobStorage uploadObj = new ExampleUploadReportToBlobStorage();
        BlobContainerClient containerClient = uploadObj.connectToAzureBlob();
        uploadObj.uploadFileToBlob(containerClient);
    }
 
    public BlobContainerClient connectToAzureBlob()
    {
        str connectionString =
            "DefaultEndpointsProtocol=https;AccountName=youraccountname;AccountKey=yourkey;EndpointSuffix=core.windows.net";
 
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("reportupload");
 
        info(strFmt("Connected to Azure Blob Container: %1", containerClient.getName()));
        return containerClient;
    }
 
    public void uploadFileToBlob(BlobContainerClient containerClient)
    {
        System.Byte[] reportBytes = ExampleUploadReportToBlobStorage::getSSRSBytes("ReportOutput");
 
        if (reportBytes)
        {
            str blobPath = "ReportUpload/UploadFile.pdf"; // folder + filename
            BlobClient blobClient = containerClient.GetBlobClient(blobPath);
 
            System.IO.MemoryStream stream = new System.IO.MemoryStream(reportBytes);
            blobClient.Upload(stream, true); // Overwrite if file exists
 
            info("File uploaded successfully to Azure Blob Storage.");
        }
    }
 
    public static System.Byte[] getSSRSBytes(str _fileName)
    {
        SrsReportRunController controller = new SrsReportRunController();
        SRSPrintDestinationSettings settings;
        System.Byte[] reportBytes;
        SRSProxy srsProxy;
        SRSReportRunService srsService = new SRSReportRunService();
        Map paramMap;
        SRSReportExecutionInfo execInfo = new SRSReportExecutionInfo();
 
        controller.parmReportName(ssrsReportStr(TransactionsReport, Report));
        controller.parmShowDialog(false);
        controller.parmLoadFromSysLastValue(false);
 
        // Set report parameters
        ReportParametersDataContract contract = controller.parmReportContract().parmRdpContract() as ReportParametersDataContract;
        contract.parmFromDate(today() - 5);
        contract.parmToDate(today());
 
        // Configure output
        settings = controller.parmReportContract().parmPrintSettings();
        settings.printMediumType(SRSPrintMediumType::File);
        settings.fileName(_fileName + ".pdf");
        settings.fileFormat(SRSReportFileFormat::PDF);
 
        controller.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
        controller.parmReportContract().parmReportExecutionInfo(execInfo);
 
        srsService.getReportDataContract(controller.parmReportContract().parmReportName());
        srsService.preRunReport(controller.parmReportContract());
 
        paramMap = srsService.createParamMapFromContract(controller.parmReportContract());
        Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] paramArray =
            SrsReportRunUtil::getParameterValueArray(paramMap);
 
        srsProxy = SRSProxy::constructWithConfiguration(controller.parmReportContract().parmReportServerConfig());
 
        reportBytes = srsProxy.renderReportToByteArray(
            controller.parmReportContract().parmReportPath(),
            paramArray,
            settings.fileFormat(),
            settings.deviceinfo());
 
        return reportBytes;
    }
}

🔐 Security Tips

  • Avoid hardcoding the connection string in production. Use Key Vault or Azure App Configuration.

  • Always restrict access to the container using RBAC or SAS tokens instead of full account keys.

  • Set your Blob container access level to Private unless you explicitly need public access.

  • Use Shared Access Signatures (SAS) or Managed Identities in production scenarios.

📌 Business Use Cases

This solution is ideal for:

  • Automatically exporting and archiving daily/weekly/monthly reports

  • Sharing output with external systems or teams via Azure Storage

  • Generating custom reports on demand and storing them securely in the cloud

✅ Summary

In this post, we’ve seen how to:

✔️ Execute an SSRS report with parameters using X++
✔️ Render it as a PDF and convert it into a byte stream
✔️ Upload the output to Azure Blob Storage securely using the latest Azure SDK

    This approach enables automated, scalable, and secure report storage in the cloud, perfect for compliance, integrations, and downstream analytics.