I need to get all field values of an specific version.
It is possible by sharepoint hosted app (javascript)?
I need to create a custom version history page of itens.
If there is a caml query solution, i think that is the best option.
It is not supported to retrieve versions for a List Item
via REST/CSOM APIs, but there are alternative options.
Versions.aspx
application pageThe idea is to perform a get request to Versions page : http://<server>/<site>/_layouts/versions.aspx?list={litsID}&ID=<itemID>
and extract versions info from page content as demonstrated below:
function getItemVersions(url,listId,itemId,success)
{
var versionsUrl = url + '/_layouts/versions.aspx?list=' + listId + '&ID=' + itemId;
$.get( versionsUrl, function( data ) {
var versionEntries = parseVersionsData(data);
success(versionEntries);
});
}
function parseVersionsData(data){
var entries = {};
var versionList = $(data).find('table.ms-settingsframe');
versionList.find('> tbody > tr').each(function(i){
if(i > 0 && (i) % 2 == 0) {
var verRow = $(this); //get version row
var propsRow = verRow.next(); //get properties row
var versionLabel = verRow.find('td:first').html().trim();
if(versionLabel.length > 0) {
entries[versionLabel] = {};
//extract item version properties
propsRow.find('tr').each(function(i){
var pName = $(this).find('td:nth-child(1)').text().trim();
var pVal = $(this).find('td:nth-child(2)').text().trim();
entries[versionLabel][pName] = pVal;
});
}
}
});
return entries;
}
Usage
var webUrl = _spPageContextInfo.webAbsoluteUrl;
var listId = _spPageContextInfo.pageListId;
var itemId = parseInt(GetUrlKeyValue('ID'));
getItemVersions(webUrl,listId,itemId,function(versionEntries){
console.log(versionEntries);
});
Another option would be to utilize Lists SharePoint Web Services that exposes Lists.GetVersionCollection Method to return version information for the specified field in a SharePoint list
SPServices example:
$().SPServices({
operation: "GetVersionCollection",
async: false,
strlistID: "Projects",
strlistItemID: 1,
strFieldName: "Description",
completefunc: function (xData, Status) {
$(xData.responseText).find("Version").each(function(i) {
console.log("Name: " + $(this).attr("Description") + " Modified: " + $(this).attr("Modified"));
});
}
});
Client Object Model doesn't have the property to retrieve the ListItem Version details.
Instead SharePoint web service provide the option to retrieve the version details of a list item. We can use the below options to get the version details.
SharePoint WebService to get Version Collection
SPservices to retrieve version history of items from SharePoint ListItem
Get old version of list item :
foreach (SPListItemVersion version in spItem.Versions)
{
//code to fetch field values
}