web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Download link for CDS ...
Power Apps
Answered

Download link for CDS attachments

(0) ShareShare
ReportReport
Posted on by

Hi,

 

I have an entity with attachments (stored in notes).

For now I've been using the attachments control in a gallery to download each file but this has limitations on styling and layout.

Instead I've created a new gallery which shows the name and thumbnail for each attachment and now I want to see if there is a way to recreate the download feature. I know there is a download function but I need a URL for each file.

 

Does anyone know how I can get/build a URL which will download a specific file stored in CDS attachments?

 

Thanks

Sam

I have the same question (0)
  • EricRegnier Profile Picture
    8,720 Most Valuable Professional on at

    Hi @Anonymous,

    You can download the note's attachment via the CDS Rest API directly from the annotation (note) entity. Here's the documentation:

    https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/reference/entities/annotation

    For example: https://<org>.api.crm.dynamics.com/api/data/v9.1/annotations(b06b2a59-9e23-4e6b-9dd7-98510563b6fa)?$select=documentbody,filename

     

    Hope this helps!

  • Community Power Platform Member Profile Picture
    on at

    Hi @EricRegnier 

     

    Thanks for this! I've managed to use the link to successfully pull the info of the file but when I click the URL it's only showing me the JSON response of the API call in the browser. The question is how do I make this link actually download the file to my PC?

     

    2020-05-20_0949.png

     

    This is what I have now from the app. Again the info it returns is correct but it just opens up a browser window with the response and doesn't actually save the file.

     

    Any ideas?

     

    Thanks

    Sam

  • Verified answer
    EricRegnier Profile Picture
    8,720 Most Valuable Professional on at

    The Download function requires a url to a remote file. The API call returns the body as base 64 string and a filename. It's not the actual URL to a file that the Download function expects. I'm not sure you can stream the file on a canvas app directly. Not ideal, but you'll have to call some kind of web service (like Azure Function) to get the file or another option is with Power Automate that saves the file to a temp location and returns in the response the location of the file.

    If those two options are not feasible, you might have to rethink about saving the files directly in CDS (which is quite costly btw) and store those in blob storage or SharePoint for example.

    Hope this clarifies a little....

  • Community Power Platform Member Profile Picture
    on at

    Got it.

     

    Thanks for the advise, I think I might reconsider where I save these files. Just wanted everything to be neatly stored in CDS but seems like it might not be the best option.

     

    Thanks

    Sam

  • Community Power Platform Member Profile Picture
    on at

    @Anonymous - I'm at the exact same situation right now, found any way around this to download the file?

    Appreciate any help.

  • sudipm Profile Picture
    Microsoft Employee on at

    Here is a solution to get download links to notes attachment in dynamics 365

     

    Create a new web resource called Download.html with the following code

     

    <!DOCTYPE html>
    
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
     <meta charset="utf-8" />
     <title></title>
     <script src="/WebResources/jquery3.6.js" type="text/javascript"></script>
     <script src="/WebResources/ClientGlobalContext.js.aspx" type="text/javascript"></script>
    </head>
    <body>
     <script type="module">
     async function DownloadFile() {
     console.log("Downloading file..")
     let urlParams = new URLSearchParams(window.top.location.search);
     if (urlParams.has("data")) {
    
     let annotId = urlParams.get("data");
     console.log("Note ID : ", annotId);
    
     let openOptions = { openMode: 2 };
     let fileOptions = {};
    
     try {
     let r = await (await fetch(`/api/data/v9.2/annotations(${annotId})?$select=documentbody,filename,mimetype,filesize`)).json();
     fileOptions = {
     fileContent: r.documentbody,
     fileName: r.filename,
     mimeType: r.mimetype,
     fileSize: r.filesize
     };
    
     console.log(r);
     }
     catch (ex1) {
     await window.top.Xrm.Navigation.openErrorDialog({ message: "File Not Found!" });
     window.top.close();
     }
     try {
     await window.top.Xrm.Navigation.openFile(fileOptions, openOptions);
     console.log("File downloaded successfully");
     
     }
     catch (ex2) {
     window.top.Xrm.Navigation.openErrorDialog({ message: "Error downloading file, please contact the Attachment owner" });
     }
     }
     else {
     window.top.close();
     }
     }
    
     $(document).ready(async function () {
     await DownloadFile();
     });
     </script>
    </body>
    </html>

     

     

    Now to generate direct download links to a note attachment from say another web resource or external application simply call the above resource with the annotation/note id as follows

     

    <a href="https://{orgname}.crm.dynamics.com/WebResources/{publishername}_Download.html?data={ANNOTATIONID}">Download File</>

     

    Replace values in the above anchor link as needed.

  • MattB-MSFT Profile Picture
    Microsoft Employee on at

    @Anonymous , if I understand your situation,  you are storing files in dataverse and are trying to create a download link for them, you are trying to get the content and not the metadata. 
    if that is the case please have a look here: https://docs.microsoft.com/en-us/power-apps/developer/data-platform/file-attributes?tabs=webapi 

    additionally,  you can use the api call GetFileSasUrl to get a Link to the file directly.  Note that link is only good for 30min. 

    hope that helps

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 739 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 343 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard