Skip to main content
Post a question

Notifications

Community site session details

Community site session details

Session Id : LUHZMFkEzowpkFU25EZ6zc
Finance | Project Operations, Human Resources, ...
Unanswered

Generating Public URL for File from DocuRef Table and Sending URL via API in D365 FO

Like (1) ShareShare
ReportReport
Posted on 26 Mar 2025 06:23:09 by 22

Hi everyone,

I'm working on a project in Dynamics 365 Finance and Operations (D365 FO) where I need to generate a public URL for a file stored in Azure Blob Storage, which is referenced in the DocuRef table, and then send this URL via an API. However, I'm facing issues with the URL generation part of my code. Below is the code I'm using:

 

class SYN_GetDatafromDocuRef
{
    // Main method to fetch the attachment URL from the document ID
    public static void main(Args _args)
    {
        // DocumentId to search for
        DataAreaId company = "syi"; // Company ID

        // Call method to fetch the URL
        str attachmentURL = SYN_GetDatafromDocuRef::fetchAttachmentURLFromDocumentId(company);

        // Print the URL to the info log
        if (attachmentURL)
        {
            info("Attachment URL: " + attachmentURL);
        }
        else
        {
            info("No URL found for the specified document and company.");
        }
    }

    // Method to fetch the attachment URL from the document ID
    public static str fetchAttachmentURLFromDocumentId(DataAreaId _company)
    {
        DocuRef docuRef;
        str getURL;
        DocuDocumentId guid1;
        guid1 = Global::guidFromString("11804389-362D-400F-A0D8-C3F5B6EAA475");

        try
        {
            // Query to fetch the DocuRef record
            select firstOnly docuRef
                where docuRef.DocumentId == guid1
                  && docuRef.ActualCompanyId == _company;

            // If the document record is found
            if (docuRef)
            {
                // Get the attachment URL by sending the file to temporary storage
                System.IO.Stream attachmentStream = DocumentManagement::getAttachmentStream(docuRef);
                str fileName = ERDocuRef_Extension::filename(docuRef);
                str strategyClass = classStr(FileUploadTemporaryStorageStrategy);

                if (attachmentStream && fileName)
                {
                    getURL = File::SendFileToTempStore(attachmentStream, fileName, strategyClass, true);
                }
                else
                {
                    // Handle error or log the issue
                    throw error("Attachment stream or filename is invalid.");
                }

                return getURL; // Return the URL
            }
            else
            {
                throw error("No document found for the specified DocumentId and company.");
            }
        }
        catch (Exception::Error)
        {
            error("An error occurred while fetching the attachment URL.");
            return ""; // Return empty string if error occurs
        }
    }
}

I'm trying to generate a public URL for the file stored in Blob Storage and send this URL via an API. The code for URL generation isn't working as expected. Could anyone provide guidance on how to properly generate a public URL for a file in Blob Storage and send it via an API in D365 FO?

Any help would be greatly appreciated!

Categories:
  • Martin Dráb Profile Picture
    232,223 Most Valuable Professional on 26 Mar 2025 at 08:08:01
    Generating Public URL for File from DocuRef Table and Sending URL via API in D365 FO
    Please use the debugger to see what happens in sendFileToTempStore(). It seems that the problem is in this condition:
    if (fileUploadResult != null && fileUploadResult.getUploadStatus())
    Find out the exact reason: whether fileUploadResult is empty or getUploadStatus() returns false. The next step will be debugging of 
     sendFileToTempStore_GetResult().
     
    Whether the approach is correct depends a lot on your business requirements (which are unknown to me). For example, the approach is wrong if you want to store the URL for later use, because it'll expire.
  • Atul_0215 Profile Picture
    22 on 26 Mar 2025 at 07:33:01
    Generating Public URL for File from DocuRef Table and Sending URL via API in D365 FO

    Thank you for your response! To clarify, I am not getting any errors in the code, but the issue is that the generated URL is empty. My goal is to retrieve a public URL for a file stored in Azure Blob Storage, which is referenced in the DocuRef table.

    I am using the File::SendFileToTempStore method to generate the URL, but it seems to return an empty value instead of a valid URL. I would appreciate any guidance on:

    1.Whether my approach to retrieving the file and generating the URL is correct.

    2.If there are alternative methods to generate a public URL for a document stored in Blob Storage within D365 FO.

  • Martin Dráb Profile Picture
    232,223 Most Valuable Professional on 26 Mar 2025 at 07:04:02
    Generating Public URL for File from DocuRef Table and Sending URL via API in D365 FO
    Please tell us more about the problem then just "I'm facing issues" and "the code isn't working as expected".

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

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... 293,325 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,223 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans
Loading started
Loading complete