Currently for DriveItem, there is no way to get a permanent link of a file, so it is not possible to insert images from OneDrive to my blog post (WordPress).
From the documentation here https://dev.onedrive.com/resources/item.htm the downloadUrl and sourceUrl will expire .
Is there a way or a plan to get a permanent file link?
Hi @AndrewLang are you targeting OneDrive personal or OneDrive for Business accounts? I ask because there is an answer for OneDrive personal, but not for OneDrive for Business.
Hi @rgregg , thanks for your reply, I do use OneDrive personal.
I suppose you mean create a sharing link mentioned in this address https://dev.onedrive.com/items/sharing_createLink.htm. I tried the embed option, it give back a url which is only able to use with iFrame, that doesn't match my use case.
My user story is.
As a blog writer, I use this tool - Matrix Writer, I want to insert images from my OneDrive.
Thanks.
Is there any updates?
Hi Andrew,
So, it's a bit convoluted, but there is a way. When you call createLink with type = embed, you get back a URL, but you also get back a shareId value, which is the key to making this work.
If you take that shareId value, and use it in the following URL, you get a URL that you can embed into a webpage / img tag directly:
https://api.onedrive.com/v1.0/shares/{shareId}/root/content
This generates a URL that will download the contents of the item (including through img tags). The URL is valid as long as the "embed link" exists, so you can always remove the permissions in the future if you want to revoke access to the file.
You can also generate thumbnail images for the file using the same URL format:
https://api.onedrive.com/v1.0/shares/{shareId}/root/thumbnails/0/medium/content
Hi @rgregg thank you very much!
With one day testing, I think this way works!
Before I asked this question, I spent more than a week to go through onedrive API documentation, there was no answer.
Just want to know is this a stable way that MS will support it for long time?
Thanks again!
Yes, this is documented here: https://dev.onedrive.com/shares/shares.htm
The shareId part isn鈥檛 well explained today because it isn鈥檛 consistent between OneDrive and OneDrive for Business but the model is something that we document and support as part of our API story.
Great, thanks a lot!
@rgregg Are the downloadUrls provided through the /shares/ endpoint permanent also? For example: https://graph.microsoft.com/v1.0/shares/{share-id}/root/ (say some @microsoft.graph.downloadUrl are present). Also, is there a difference in the lifetime of a share direct link when created through the graph api vs the FilePicker SDK (action:"share")?
edit I think I figured it out. The file picker provides download urls with public. subdomain while the graph /createLink provides ones with non-public subdomains. So I believe it's unavoidable to do at least 1 request for creating a share link.
No solution for OneDrive for business? I am hoping to embed links to documents in Visual Studio Team Services cards but if they expire it would be less useful...
@rgregg Is there a similar trick for OneDrive for Business?
https://api.onedrive.com/v1.0/shares/{shareId}/root/content
Sorry to reply to this three years later but I'm trying to implement this trick myself. Your post says that the https://api.onedrive.com/v1.0/shares/{shareId}/root/content will generate "a URL that will download the contents of the item".
Is that in contrast to the documentation here for getting the content of image thumbnails, where it says that the response will be the binary content of the image?
I ask because I'm treating the response from https://api.onedrive.com/v1.0/shares/{shareId}/root/content as binary content and trying to use it in an <img> tag (e.g. <img src="data:image/jpg;base64,{response}">) but not having much success.
I have OneDrive for Business, and when I call /CreateLink with type view and scope anonymous I don't get the shareId at all.
It is forcing me to use the webUrl. However, I wish I was able to call /Shares with the shareId and access the file directly.
@rgregg Is there any solution for OneDrive Business? Thank you.
Most helpful comment
Hi Andrew,
So, it's a bit convoluted, but there is a way. When you call createLink with type = embed, you get back a URL, but you also get back a
shareIdvalue, which is the key to making this work.If you take that shareId value, and use it in the following URL, you get a URL that you can embed into a webpage / img tag directly:
This generates a URL that will download the contents of the item (including through img tags). The URL is valid as long as the "embed link" exists, so you can always remove the permissions in the future if you want to revoke access to the file.
You can also generate thumbnail images for the file using the same URL format: