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, ...
Answered

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

(2) ShareShare
ReportReport
Posted on by 60

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:
I have the same question (0)
  • Martin Dráb Profile Picture
    237,898 Most Valuable Professional on at
    Please tell us more about the problem then just "I'm facing issues" and "the code isn't working as expected".
  • Atul_0215 Profile Picture
    60 on at

    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
    237,898 Most Valuable Professional on at
    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.
  • Verified answer
    Nisarg  Jani Profile Picture
    527 on at
    Hi,
     
    The above Code seems fine, For the URL you can also use File::SendFileToUser function. Make sure your file is proper and valid stored in Docu Table.
     
    Regards,
    Nisarg Jani
  • Atul_0215 Profile Picture
    60 on at
    Hi Nisarg,
     
    thank you for your response. As you suggested, I checked my table and reviewed the file attachment data source. I found an issue—the file field only contains text and no actual attachment. Your observation really helped me. Thank you!

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 544 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans