How to download OneDrive/SharePoint attachments off messages (email) using Graph API?

eggy mcegg 120 Reputation points
2024-07-23T17:34:24.4+00:00

I have a requirement where I have to download the content that is attached to the emails in my organisation.

Scenarios include

  • when a user is on OneDrive/Sharepoint and they go to share a file via email. A 'sent' email gets generated in said users emails.
  • when a user is on Outlook -> New message -> Attach file -> OneDrive -> Send. A 'sent' email gets generated in said users emails.

I can access these emails, but the problem is I am unable to download the actual content. For instance the attachments endpoint (when I share via OneDrive):

https://graph.microsoft.com/v1.0/users/{user-id}/messages/{msg-id}/attachments

Returns the actual attachments on the email (i.e. images). I am able to extract a href off the body content but I do not know how to download the content that is linked? i.e.

https://sharepoint-my.sharepoint.com/:x:/g/personal/administrator/<id?>>?e=4%3a0uILKI&amp;at=9\

When I attach when a new email is created:

https://sharepoint.sharepoint.com/:w:/t/TestTree/<id?>

Again I don't know how to download the content that is being linked in the href.

Any idea how I can do this guys? Thanks

Outlook
Outlook
A family of Microsoft email and calendar products.
3,991 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,254 questions
OneDrive
OneDrive
A Microsoft file hosting and synchronization service.
1,139 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,813 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yanli Jiang - MSFT 26,761 Reputation points Microsoft Vendor
    2024-07-25T02:38:31.3333333+00:00

    Hi @eggy mcegg ,

    Great to know that and thanks for sharing the update here.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others.". and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    [How to download OneDrive/SharePoint attachments off messages (email) using Graph API?]

    Issue Symptom:

    I have a requirement where I have to download the content that is attached to the emails in my organization.

    Scenarios include

    • when a user is on OneDrive/Sharepoint and they go to share a file via email. A 'sent' email gets generated in said users emails.
    • when a user is on Outlook -> New message -> Attach file -> OneDrive -> Send. A 'sent' email gets generated in said users emails.

    I can access these emails, but the problem is I am unable to download the actual content. For instance the attachments endpoint (when I share via OneDrive):

    https://graph.microsoft.com/v1.0/users/{user-id}/messages/{msg-id}/attachments
    

    Returns the actual attachments on the email (i.e. images). I am able to extract a href off the body content but I do not know how to download the content that is linked? i.e.

    https://sharepoint-my.sharepoint.com/:x:/g/personal/administrator/<id?>>?e=4%3a0uILKI&amp;at=9\
    

    When I attach when a new email is created:

    https://sharepoint.sharepoint.com/:w:/t/TestTree/<id?>
    

    Again I don't know how to download the content that is being linked in the href.

    Any idea how I can do this guys? Thanks

    Current status:

    Ok I managed to find a solution by using shared links

    https://learn.microsoft.com/en-us/graph/api/shares-get?view=graph-rest-1.0&tabs=http

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community members to see the useful information when reading this thread. Thanks for your understanding!

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. eggy mcegg 120 Reputation points
    2024-07-24T08:33:43.2+00:00
    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.