How to get binary format from Microsoft Garph API into other formats?

Nina Schmidt 20 Reputation points
2023-04-18T07:45:05.12+00:00

I want to get the data of a PDF which lies in sharepoint and get its content. Then use the content to show the PDF in an application with PDF.js. For this I use the a call in this format: GET /sites/{siteId}/drive/items/{item-id}/content. The content is returned as binary. But what kind of binary? How can I convert the data to further usage? https://learn.microsoft.com/en-us/graph/api/driveitem-get-content?view=graph-rest-1.0&tabs=http Is there the possibility I can get the data in a different format for example JSON?

Microsoft 365 and Office SharePoint Development
Microsoft Security Microsoft Graph
{count} votes

Accepted answer
  1. msft-gu 1,355 Reputation points
    2023-04-18T19:07:10.9666667+00:00

    Hi Nina, Good afternoon and I hope you are doing well! Thank you for your question. I tested GET https://graph.microsoft.com/v.1/me/drive/items/{item-id}/content and I was able to get response. On Graph Explorer, I received an error due to CORS policy although I was able to run this on Postman. On Postman:

    • if the file is PDF I was able to view it graphically.
    • if the file is other than pdf, I was able to view it either binary or unreadable characters. Using the steps provide in the article Download a file in another format, I was able to view a docx file on PDF format. There are certain notes when using it according to the ar:
    • This endpoint requires TLS 1.2 to function normally.
    • Not all files can be converted into all formats.
    • Here are the only supported source extensions: csv, doc, docx, odp, ods, odt, pot, potm, potx, pps, ppsx, ppsxm, ppt, pptm, pptx, rtf, xls, xlsx .

    <p>For download files in JavaScript apps, please refer to Downloading files in JavaScript App under the Download the contents of a DriveItem.</p>

    <p> Since we are limited on source and destination file type, I would suggest providing a feedback at https://feedbackportal.microsoft.com/feedback/forum/ebe2edae-97d1-ec11-a7b5-0022481f3c80 so that our team can look into it and add the feature</p>

    Reference: Download the contents of a DriveItem https://learn.microsoft.com/en-us/graph/api/driveitem-get-content-format?view=graph-rest-1.0&tabs=http

    Download a file in another format https://learn.microsoft.com/en-us/graph/api/driveitem-get-content-format?view=graph-rest-1.0&tabs=http


1 additional answer

Sort by: Most helpful
  1. Jhonsmint 0 Reputation points
    2023-10-18T01:59:49.6433333+00:00

    To convert data from the Microsoft Graph API into binary format and subsequently into other formats, you'll typically need to follow these steps:

    Accessing Data from Microsoft Graph API: First, you need to authenticate and make requests to the Microsoft Graph API to retrieve the data you require. This often involves using OAuth 2.0 for authentication and sending HTTP requests to the API endpoints. The API may return data in various formats, such as JSON.

    Parse the API Response: The data you receive from the Microsoft Graph API is often in JSON format. To convert it to binary format, you'll need to parse the JSON data to extract the content you want to convert.

    Binary Conversion: Depending on the specific data you're dealing with, the process of converting it to binary format may vary. Here are a few common scenarios:

    File Downloads: If you're dealing with files (e.g., documents, images, videos), you may need to download the file content as binary data. The API may provide a URL or a direct binary representation. You can use libraries or programming languages like Python to download and save this data in binary format.

    Base64 Encoding: In some cases, data may be available as Base64-encoded strings. You can decode these strings to obtain the binary data. Most programming languages provide built-in functions or libraries to handle Base64 encoding and decoding.

    Custom Data: For custom data structures, you may need to follow specific conversion procedures based on the data's structure. This could involve serializing the data into binary format using language-specific serialization libraries or protocols like Protocol Buffers or MessagePack.

    Conversion to Other Formats: Once you have the data in binary format, you can convert it to other formats based on your requirements. Common formats include:

    Text/Strings: If you have binary data that represents text, you can convert it into text format using character encoding (e.g., UTF-8).

    Images: Binary image data can be converted into various image formats like PNG, JPEG, or GIF using appropriate libraries or tools.

    Documents: Binary data representing documents (e.g., PDFs or Word documents) may need to be processed using specialized libraries or tools for document conversion.

    Audio/Video: Binary audio or video data can be converted into various audio/video formats using appropriate codecs and libraries.

    Error Handling and Data Validation: Ensure error handling mechanisms are in place to handle any issues that may arise during data conversion. Validate the data to ensure its integrity and correctness.

    Output and Storage: Finally, the converted data can be used as needed, whether it's for display, storage, or further processing.

    Remember that the specific implementation details may vary depending on the programming language and tools you are using. Additionally, consider the Microsoft Graph API's documentation for any specific guidelines on handling binary data for the endpoints you are working with.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.