0

We want to access last major version of page with all its OOTB & custom fields using JSOM/REST. I can able to get only version number & url only but we need all fields.

Please help. Thank you.

1 Answer 1

0

In SharePoint Online, we can use Lists.GetVersionCollection method to return version information for an item in a SharePoint list/library. But it only returns version information for the specified field in a SharePoint list/library. What’s more, there is no such function in the Lists.asmx can help us to get all the versions of all fields in a batch, so we might need to iterate through all the wanted fields one by one.

Refer to my answer in this thread: Add Version History details in dispform or editform.aspx

To get the latest major version, you can check the Version values from version collection of Version column. If it is integer, it means it is a major version.

Here is a demo about how to get the last major version for an item in a list/library. For all items, you can retrieve all item ids and execute the commands for each item id.

<div id="displayDiv"></div>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.1a/jquery.SPServices-0.7.1a.min.js"></script>
<script type="text/javascript">
var currentItemId = "<item ID>"; //you can also loop all item ids from the list/library
var table="<table><tr><th>Version</th><th>Title</th><th>Editor</th><th>Modified</th></tr>";
var html ="";
var fieldname ="";
$(function(){
                var oTitle = getFieldVersion("Title");
                var oVersion = getFieldVersion("Version");
                var oModified = getFieldVersion("Modified");
                var oEditor = getFieldVersion("Editor");
                for (var i = 0; i < oTitle.length; i++) {
                    var major = Number.isInteger(parseFloat(oVersion[i]));
                    if(major){
                        html += "<tr>";
                        html += "<td>"+ oVersion[i] +"</td>";
                        html += "<td>" + oTitle[i] +"</td>";
                        html += "<td>" + oEditor[i].split("#")[1] +"</td>";
                        html += "<td>" + oModified[i] +"</td>";
                        html += "</tr>";
                        break;
                    }

                }
    document.getElementById("displayDiv").innerHTML=table+html+"</table>";            
});

function getFieldVersion(fieldname){
                var versionContent = [];
                $().SPServices({
                                                operation: "GetVersionCollection",
                                                async: false,
                                                strlistID: "<list/library>",
                                                strlistItemID: currentItemId,
                                                strFieldName: fieldname,
                                                completefunc: function (xData, Status) {
                                                console.log(xData);
                                                console.log($(xData.responseText).find("Version"));
                                                $(xData.responseText).find("Version").each(function(i) {
                                                  console.log($(this).attr(fieldname) );
                                                  versionContent.push($(this).attr(fieldname));
                                                });  

                                                }
                                }); 
                return versionContent;                                                 
}
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.