SOLVED

How to get current version of a document on Sharepoint?

Brass Contributor

How can I get the current version of a document on Sharepoint?  I've successfully done a GET List Versions, GET Version and GET Major Version but none of them return the current version.

 

21 Replies

Hey @Si_Da,

 

The version of a document is given by the OData__UIVersionString parameter returned in the REST call.  This answer shows how to query the version for a specific documents or all documents in a list or library.

 

Regards.

@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? 

@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.

 

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.

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.

@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!

PS: do you know what the difference is between UIVersionLabel and VersionLabel?

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.

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!

best response confirmed by Si_Da (Brass Contributor)
Solution

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.

@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?

Hey @Si_Da ! No worries, hope you're better now.

 

Back to the problem, so you're able to make the REST calls through testing but not through AJAX? I'd reccommend checking your header just in caso, if that's  the scenario.

 

Regards,

 

 

@Deleted 

 

I've not added anything to the header and yet it is only these 2 calls that are not working (getting previous versions and getting current version), except of course when testing at design time and then they do work.  It's so frustrating as I have no idea what could be causing this as all the permissions will be the same for all the calls.

....and also Restore Version is not working either (well at runtime anyway, works fine testing at design time).  All other REST APIs using the same parameters from the same app are working fine at runtime.

 

I have no idea what could be causing the 403 Forbidden for all these version related REST APIs.  Although they were all created on the same day but I can't see anything different to any others.  I'm going to experiment with some more calls and report back....

Ok so I have a REST API that just checks for the existence of a document and it is identical to the one I'm using to get versions except it has '/versions' on the end of it.  The one that checks for the existence of a document works fine so I cut and pasted that into a new REST call (including the parameters and how it's called from within the app) and then I added '/versions' on the end and that still generates the 403 Forbidden error.  So it seems that there is some restriction with anything relating to versions.  Although I don't know why, nor why the app can test the REST calls during design time and work just fine.  Very confusing!

The app can successfully delete files and overwrite them, check in and out etc.  I have also tried checking a file out before reading the version information just in case (as check out is forced before any actions can be carried out on this site) but that didn't work either.

Hey @Si_Da ,

 

That's really odd. I tried searching for this specific problem and nothing came up. I suggest that you create a new question on this board, maybe StackOverflow too, to get more information.

 

Also, how is your app running in SPO? Is it a just an embedded script? Can you share your request so I can try it out in my environment?

 

Regards,

@Deleted  yes I tried searching too and found nothing and I've already created a separate post on here but no response, will try StackOverFlow too though, thanks.

 

I'm running the script through Outsystems v10 integrating with SharePoint online.  If I can't get it working I'm considering seeing if I can wrap it in C# and integrate that component into Outsystems just to see if it makes any difference, not that it should of course but not sure what else to try.  This is one of the calls I'm using that generates the 403 Forbidden....

 

https://MyCompany.SharePoint.com/sites/{SiteName}/_api/web/GetFileByServerRelativeUrl('/sites/{SiteName}/Shared%20Documents/{FilenameAndPath}')/versions

1 best response

Accepted Solutions
best response confirmed by Si_Da (Brass Contributor)
Solution

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.

View solution in original post