Hi @Denny ,
For adding OneDrive file link as an attachement to a mail, SourceUrl and ProviderType properties must to be set as part the request body. According to this documentation, currently these properties are only available in Microsoft Graph Beta API but not in V1.0 API. In order to call the beta API, you would have to install & use the Microsoft.Graph.Beta package. You can refer this link on Microsoft Graph Beta .NET Client Library (SDK) installation and other details.
Below Microsoft Graph Beta API endpoint is used to attach OneDrive file link to a mail by setting SourceUrl and ProviderType properties in request body :
POST https://graph.microsoft.com/beta/users/{user-id}/messages/{message-id}/attachments
Important Note :
APIs under the /beta version in Microsoft Graph are subject to change. Use of these APIs or relevant Beta SDK client libaries in production applications is not supported.
Here is the sample code snippet for attaching OneDrive file link to reference object using Beta API or Microsoft.Graph.Beta package & C#.NET SDK :
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var attachment = new ReferenceAttachment
{
Name = "OneDriveAttachment",
SourceUrl = "https://o365-my.sharepoint.com/personal/jxxxx_o365_onmicrosoft_com/Documents/Test.docx",
ProviderType = ReferenceAttachmentProvider.OneDriveConsumer,
IsFolder = false
};
await graphClient.Users["abcde-90f8-4088-ad78-38d45cbf3f1a"].Messages["AAMkADMyMDI2MjY5LTc1MzgtNDhiNi1iM2E0LTk0MmUzNzhhMmQyMgBGAAAAAAB8OAEgBrpTSrTOnU1piS01BwBmL_k7qYXPRZQMwo9Nyp1dAAAAAAEPAABmL_k7qYXPRZQMwo9Nyp1dAAGyabceAAA="].Attachments
.Request()
.AddAsync(attachment);
I did also verified the above 3 step solution approach using Graph API and it worked with Beta endpoint to attach OneDrive file link attachment with a message / mail. Here are my testing screenshots :
POST https://graph.microsoft.com/v1.0/users/{user-id}/messages
POST https://graph.microsoft.com/beta/users/{user-id}/messages/{message-id}/attachments
POST https://graph.microsoft.com/v1.0/users/{user-id}/messages/{message-id}/send
Final Output of Sent Email (with OneDrive file link attachement) :
You can refer below addtional documentation link for more information and quick comparision :
Reference attachment type (V1.0 API) : https://learn.microsoft.com/en-us/graph/api/resources/referenceattachment?view=graph-rest-1.0
Reference attachment type (Beta API) : https://learn.microsoft.com/en-us/graph/api/resources/referenceattachment?view=graph-rest-beta
Microsoft Graph Beta .NET Client Library (SDK) : https://github.com/microsoftgraph/msgraph-beta-sdk-dotnet
Hope this helps.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have further questions about this answer, please click "Comment".