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.
Deletedfirstly thank you so much for replying!
I have already looked at that example, however it is retrieving the information from a list and I want to specify the filename, e.g.
.../_api/web/getFileByServerRelativeUrl('/sites/{SiteName}/Shared%20Documents/{FilenameAndPath}')/...
So, is there a constraint from using the Item ID instead of the File Path? Getting the Item gives you advantages such as getting all the versions, if you wish to.
Anyway, this query you mentioned above should have the UIVersionLavel field, which contains the current version for the Item, right?
- Si_DaApr 11, 2019Brass Contributor
Deleted thanks for your reply.
I'm using the following to get previous versions:
https://{CompanyName}.sharepoint.com/sites/{SiteName}/_api/web/GetFileByServerRelativeUrl('/sites/{SiteName}/Shared%20Documents/{FilenameAndPath}')/versions
I need to have 2 separate functions for getting the current version and previous versions and that returns the UIVersionLabel. Is there a benefit to doing it the way you suggest? I have not worked with items before and don't know how the item number relates to any given file?
Also I don't suppose you know how to restore to a previous version by any chance please? The following POST generates an error saying it's not valid:
https://{CompanyName}.sharepoint.com/sites/{SiteName}/_api/web/GetFileByServerRelativeUrl('/sites/{SiteName}/Shared%20Documents/{FilenameAndPath}')/versions/{VersionID}/restoreVersion
I've tried putting the Version ID in curly brackets and with/without the preceding forward slash but it still doesn't work.
- Si_DaApr 11, 2019Brass Contributor
Actually I just remembered, the above doesn't return the UIVersionLabel, although it does return the VersionLabel which I presume is the same thing? I did do something that returned a whole load of info (more than the REST call above) and the results included the UIVersionLabel, but I can't remember what I did now. But of course for restoring purposes it's the ID that I need.
- AnonymousApr 11, 2019
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.
- Si_DaApr 11, 2019Brass Contributor
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!