SOLVED

Token doesn't allow to access version history of files

Copper Contributor

Hello,

 

We are using SharePoint API to develop an integration with SharePoint for our software. We have a problem with getting the version history of files: the token we receive via the API allows to read files, but doesn't allow to access vti history (like https://revizto.sharepoint.com/_vti_history/2048/Shared Documents/Document23.docx). If we login to SharePoint itself via browser for the same user, it allows to see version history there. But via the API we cannot do this.

 

Did anyone face similar issues?

Thanks!

4 Replies
What is the context? SPFx? SP Addin?
best response confirmed by azrevizto (Copper Contributor)
Solution

@azrevizto 

 

I don't know if you are using a Provided or Hosted AddIn, but try this;

 

function getUrlParameter(name) {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var results = regex.exec(decodeURIComponent(location.search));
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}

 

Not get the stuff

 

var itemID = getUrlParameter("ID");
    var versionID = getUrlParameter("VersionNo");
    if(itemID!= "" && versionID!= ""){
        $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/lists/getbytitle('"+libraryName+"')/Items("+itemID+")/versions("+versionID+")?$select=FileLeafRef,FileRef",
            type: "GET",
            headers: {
                "Accept": "application/json;odata=verbose",
            },
            success: function (data) {
                var versionFileUrl = data.d.FileRef.replace(libraryName,"_vti_history/"+versionID+"/"+libraryName);
            },
            error: function (data) {
               
            }
        });
    }

 

Or CSOM see:

 

New SharePoint CSOM version released for SharePoint Online - September 2017 - Microsoft 365 Develope...

 

Thanks a lot, your hint lead us to the right answer, which was to use the link of the following type:

https://revizto.sharepoint.com/_api/web/GetFileByServerRelativeUrl(‘/Shared Documents/Document23.docx’)/Versions/GetById(1024)/$value
1 best response

Accepted Solutions
best response confirmed by azrevizto (Copper Contributor)
Solution

@azrevizto 

 

I don't know if you are using a Provided or Hosted AddIn, but try this;

 

function getUrlParameter(name) {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var results = regex.exec(decodeURIComponent(location.search));
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}

 

Not get the stuff

 

var itemID = getUrlParameter("ID");
    var versionID = getUrlParameter("VersionNo");
    if(itemID!= "" && versionID!= ""){
        $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/lists/getbytitle('"+libraryName+"')/Items("+itemID+")/versions("+versionID+")?$select=FileLeafRef,FileRef",
            type: "GET",
            headers: {
                "Accept": "application/json;odata=verbose",
            },
            success: function (data) {
                var versionFileUrl = data.d.FileRef.replace(libraryName,"_vti_history/"+versionID+"/"+libraryName);
            },
            error: function (data) {
               
            }
        });
    }

 

Or CSOM see:

 

New SharePoint CSOM version released for SharePoint Online - September 2017 - Microsoft 365 Develope...

 

View solution in original post