Get current SharePoint file version using REST call
-
10-10-2020 - |
Вопрос
I use a REST call to get file information as:
http://MY_WEB_SITE/_api/web/lists/getByTitle('Documents')/items?$select=FileLeafRef
Can I get the current file version using same REST call? If so how ?
This didn't workout
http://MY_WEB_SITE/_api/web/lists/getByTitle('Documents')/items?$select=FileLeafRef,_UIVersionString
Решение
Here is the RESTful URL you need to get the current file version of a specific file (by its ID):
http://MY_WEB_SITE/_api/web/lists/getByTitle('Documents')/items(1)?$select=FileLeafRef,OData__UIVersionString
Here is the RESTful URL you need to get the current file version for all files in the library:
http://MY_WEB_SITE/_api/web/lists/getByTitle('Documents')/items?$select=FileLeafRef,OData__UIVersionString
Другие советы
You can get the specific file's version using REST call by below given code.
// Get the file versions
function getFileVersions() {
var getfileurl = document.getElementById("getfileurl").value;
var executor;
// Initialize the RequestExecutor with the app web URL.
executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
url: appweburl + "/_api/SP.AppContextSite(@target)/web/GetFileByServerRelativeUrl('" + getfileurl + "')/Versions?@target='" + hostweburl + "'",
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
success: getFileVersionsSuccessHandler,
error: getFileVersionsErrorHandler
});
}
// Success Handler
function getFileVersionsSuccessHandler(data) {
var versions = 'File Versions:\n'
var jsonObject = JSON.parse(data.body);
var results = jsonObject.d.results;
for (var i = 0; i < results.length; i++) {
versions += results[i].VersionLabel + '\n';
}
// Display the File versions
alert(versions);
}
// Error Handler
function getFileVersionsErrorHandler(data, errorCode, errorMessage) {
alert("Could not get the file versions: " + errorMessage);
}
Note:- Kindly change the variable values as per your environment.
Не связан с sharepoint.stackexchange