Where can I find the contentBytes of a document I have stored in a sharepoint site?

Trevor Westin 26 Reputation points
2022-06-07T20:53:26.257+00:00

I'm using graph API to auto-populate an email with attachments (Documents from SharePoint site) and need to know how I can access the contentBytes of these documents.

Thanks!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,662 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,692 questions
{count} votes

2 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 31,606 Reputation points Microsoft Vendor
    2022-06-08T08:56:44.337+00:00

    Hi @Trevor Westin ,
    You can expand File Size to retrieve file size by following url

    https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items?expand=fields(select=FileLeafRef,File_x0020_Size)  
    

    209463-image.png
    Here is the document for refer
    https://learn.microsoft.com/en-us/graph/api/listitem-list?view=graph-rest-1.0&tabs=http


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  2. Trevor Westin 26 Reputation points
    2022-06-22T13:10:59.183+00:00

    If you stringify() the driveItem you can splice out the download url. Then using the code below you can make a blob file from the url where you can then grab the content bytes of that blob file.

      async function getBase64(fileDownloadUrl, onLoadCallback) {  
        let blob = await fetch(fileDownloadUrl).then(r => r.blob());  
        return new Promise(function (resolve, reject) {  
          var reader = new FileReader();  
          reader.onload = function () { resolve(reader.result); };  
          reader.onerror = function () { setEmailError(true) };  
          reader.readAsDataURL(blob);  
        });  
      }