Forum Discussion
How to get current version of a document on Sharepoint?
- AnonymousApr 11, 2019
Hey Si_Da ,
Just figured how you can get the Item versions from the URL.
You'll need 2 REST calls.
The first is _api/web/getFileByServerRelativeUrl('{filePath}')/listItemAllFields. This call should give you some important info about the file, including the field "OData__UIVersionString" that I first mentioned, which give you the current file version. If you want to get all versions from there, you can use the field "odata.editLink", which contains the an URL string to the Item containing the file. So you can just append this URL to your site relative URL and add "/versions" to the end. Something like "tenant.sharepoint.com/sites/{sitename}/_api/Web/Lists(listID)/Items({itemID})/versions"
let me know if it helps.
Hey Si_Da,
First, I will assume you are using PnPJS to build the queries for simplicity. If not, you can figure what the REST calls are from the code, although I strongly recommend you to use it.
One of the advantage of using item instead of getFileByServerRelativeUrl is that you can get the item versions all at once, for example. The query sp.web.lists.getByTitle("Shared Documents").items.getById({itemID}).versions will return the previous and current versions of the document.
To get the Item from a Document, you can just call ...getFileByServerRelativeUrl("{filePath}").getItem() and with the result you can can call versions.get().
The restore an Item, you can call ...getFileByServerRelativeUrl('{filePath}')/versions/restoreByLabel(versionlabel='{version}') (or ""). That worked for me.
Lemme know if you have any troubles.
Deleted Thanks again for your response.
I'm not using PnPJS, just writing it all by hand, although I have Postman that I can use to check they're working correctly, as well as the platform I'm integrating into.
I need to have a URL though, so can sp.web.lists.getByTitle be used in that way as none of the examples I looked at did? Although the Restore Version is working now (thank you!!) and it is much better using the label rather than the ID (which was how the documentation I read instructed to do it). So now all I need is to be able to read the current version.
Thanks again, much appreciated!
- AnonymousApr 11, 2019
Hey Si_Da, Glad I could help with the Restore Version!
I agree that using this approach instead of retrieving by ID is much easier when restoring an Item. However, to get current and previous versions I would suggest you to use the Item, as its request return a lot more info than the file version. You can even get it only using the URL as you are right now.
However, if you don't use the file ID, or at least the PnP library, I am not sure what approach you can take. I would suggest you try using this library to at least get these versions, and I believe you wouldn't go back to manual REST calls once you get to use it. But if you find another way, lemme know.
BTW, I think UIVersionLabel and VersionLabel are the same thing, too. Not sure, though.
- Si_DaApr 11, 2019Brass Contributor
I only need version information (at this time at least) but sure I'll give it a go! Please could you give me an example or some pointers though as I've no idea how to go about using it?
Thanks again and for your patience!
- Si_DaApr 11, 2019Brass ContributorPS: do you know what the difference is between UIVersionLabel and VersionLabel?
- AnonymousApr 11, 2019
Hey Si_Da ,
Just figured how you can get the Item versions from the URL.
You'll need 2 REST calls.
The first is _api/web/getFileByServerRelativeUrl('{filePath}')/listItemAllFields. This call should give you some important info about the file, including the field "OData__UIVersionString" that I first mentioned, which give you the current file version. If you want to get all versions from there, you can use the field "odata.editLink", which contains the an URL string to the Item containing the file. So you can just append this URL to your site relative URL and add "/versions" to the end. Something like "tenant.sharepoint.com/sites/{sitename}/_api/Web/Lists(listID)/Items({itemID})/versions"
let me know if it helps.
- Si_DaApr 17, 2019Brass Contributor
Deleted
Sorry for the delayed reply (was unwell and then really busy)....
That's great, thank you! I already have a way to get the previous versions but "_api/web/getFileByServerRelativeUrl('{filePath}')/listItemAllFields" works to obtain the current one. :) Very odd to me that Get Versions and Get Major Version don't return the current version but no matter, at least it works now.....
....well kind of! I can do test calls and getting the version works fine. However when I run it through my application, it returns a 403 Forbidden error. What is odd is that all other REST calls from my application to SharePoint are working just fine, it's only the Get Version calls that aren't. I don't suppose you have any idea what could be causing this?