none
Getting list item versioning by JSOM RRS feed

  • Question

  • In a SharePoint hosted app in a SharePoint online (SPO), I have to retrieve (for a simple print out page/report) a list of tasks, and retrieve versioned history of the task (as the user see when he/she edit an item).

    I have realized, that I have to roll up all versions and read "Body" to get the same list.

    But is it possible using JSOM from a SharePoint hosted app (aspx) page?

    (I'm using CAML to retrieve my items).



    Jorgen

    Monday, September 1, 2014 3:11 PM

Answers

  • Hi,

    According to your post, my understanding is that you want to get list item version using JavaScript Client Object Model.

    The following code snippet for your reference:

    that.getData = function (listName, xmlQuery, contentType, onGetSuccess) {
        var objClientCtx = new SP.ClientContext.get_current();
        if (objClientCtx) {
            var oWeb = objClientCtx.get_web();
            var oList = oWeb.get_lists().getByTitle(listName);                
            var query = new SP.CamlQuery();
            query.set_viewXml(xmlQuery);
            var objlistItems = oList.getItems(query);
            objClientCtx.load(objlistItems);				
            objClientCtx.executeQueryAsync(function (sender, args) {
                that.DataSet = [];
                var objlistEnumerator = objlistItems.getEnumerator();
                while (objlistEnumerator.moveNext()) {
                    var objListItem = objlistEnumerator.get_current();
                    var id = objListItem.get_item('ID');
                    var filePath = 'your site collection/Lists/your list/'+id+'_.000'
                    var web = objClientCtx.get_web();
                    var listItemInfo = web.getFileByServerRelativeUrl(filePath)
                    var listItemFields = listItemInfo.get_listItemAllFields()
                    objClientCtx.load(web);
                    objClientCtx.load(listItemInfo);
                    objClientCtx.load(listItemFields);
                    //objClientCtx.load(versions1);
                    objClientCtx.executeQueryAsync(
    				    function (sender, args) {
    					    var fileVersions = listItemInfo.get_versions();
    					    objClientCtx.load(fileVersions);
    					    objClientCtx.executeQueryAsync(
    						    function (sender, args) {
    							    var objlistVersionEnumerator = fileVersions.getEnumerator();
    							    while (objlistVersionEnumerator.moveNext()) {
    								    var objCurrentListItemVersion = objlistVersionEnumerator.get_current();
    								    console.log(objCurrentListItemVersion.get_url());
    							    }
    									
    						    },
    						    function (sender, args) {console.log('Error');}
    					    )
    							
    				    }, 
    				    function (sender, args){
    					    console.log('error')
    				    });                        	
    	
                }
                onGetSuccess(that.DataSet);
    	
            }, function (sender, args) {
                that._onGetFail(sender, args);
            });
        }
    }

    More information is here:
    http://techfindings-prem.blogspot.com/2014/06/how-to-get-all-versions-of-list-using.html 


    Thanks,
    Dennis Guo
    TechNet Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Dennis Guo
    TechNet Community Support

    • Marked as answer by Jørgen Thyme Monday, September 8, 2014 9:11 PM
    Tuesday, September 2, 2014 3:18 AM
    Moderator

All replies

  • Hi,

    According to your post, my understanding is that you want to get list item version using JavaScript Client Object Model.

    The following code snippet for your reference:

    that.getData = function (listName, xmlQuery, contentType, onGetSuccess) {
        var objClientCtx = new SP.ClientContext.get_current();
        if (objClientCtx) {
            var oWeb = objClientCtx.get_web();
            var oList = oWeb.get_lists().getByTitle(listName);                
            var query = new SP.CamlQuery();
            query.set_viewXml(xmlQuery);
            var objlistItems = oList.getItems(query);
            objClientCtx.load(objlistItems);				
            objClientCtx.executeQueryAsync(function (sender, args) {
                that.DataSet = [];
                var objlistEnumerator = objlistItems.getEnumerator();
                while (objlistEnumerator.moveNext()) {
                    var objListItem = objlistEnumerator.get_current();
                    var id = objListItem.get_item('ID');
                    var filePath = 'your site collection/Lists/your list/'+id+'_.000'
                    var web = objClientCtx.get_web();
                    var listItemInfo = web.getFileByServerRelativeUrl(filePath)
                    var listItemFields = listItemInfo.get_listItemAllFields()
                    objClientCtx.load(web);
                    objClientCtx.load(listItemInfo);
                    objClientCtx.load(listItemFields);
                    //objClientCtx.load(versions1);
                    objClientCtx.executeQueryAsync(
    				    function (sender, args) {
    					    var fileVersions = listItemInfo.get_versions();
    					    objClientCtx.load(fileVersions);
    					    objClientCtx.executeQueryAsync(
    						    function (sender, args) {
    							    var objlistVersionEnumerator = fileVersions.getEnumerator();
    							    while (objlistVersionEnumerator.moveNext()) {
    								    var objCurrentListItemVersion = objlistVersionEnumerator.get_current();
    								    console.log(objCurrentListItemVersion.get_url());
    							    }
    									
    						    },
    						    function (sender, args) {console.log('Error');}
    					    )
    							
    				    }, 
    				    function (sender, args){
    					    console.log('error')
    				    });                        	
    	
                }
                onGetSuccess(that.DataSet);
    	
            }, function (sender, args) {
                that._onGetFail(sender, args);
            });
        }
    }

    More information is here:
    http://techfindings-prem.blogspot.com/2014/06/how-to-get-all-versions-of-list-using.html 


    Thanks,
    Dennis Guo
    TechNet Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Dennis Guo
    TechNet Community Support

    • Marked as answer by Jørgen Thyme Monday, September 8, 2014 9:11 PM
    Tuesday, September 2, 2014 3:18 AM
    Moderator
  • check this

    http://techfindings-prem.blogspot.in/2014/06/how-to-get-all-versions-of-list-using.html

    http://sharepoint.stackexchange.com/questions/85887/how-to-get-version-history-of-all-fields-updated-for-a-list-item

    Tuesday, September 2, 2014 6:54 AM
  • Brilliant - I'll buy you a large beer when you get to CPH


    Jorgen

    Monday, September 8, 2014 9:12 PM
  • Hi, I am unable to get createdBy.get_title() from FileVersion object I get createdBy as SP.User object. Even I tried to load createdBy but failed. Below is my code. Please help.

    function onGetVersionHistorySuccess(fileVersions) {
            var objlistVersionEnumerator = fileVersions.getEnumerator();
            totalVersions = fileVersions.get_count();        
            vTableHtml = "";

            while (objlistVersionEnumerator.moveNext()) {
                var vItem = objlistVersionEnumerator.get_current();
                var createdBy = vItem.get_createdBy(); //Gets SP.User object

                var createdBy = createdBy.get_title(); //Code fails here. Title not initialize. Even I tried to load createdBy object but failed.

            }

    }

    Please help.

    Thursday, March 1, 2018 9:44 AM
  • Hi Denis,

    1-I tried to implement the same and modified the code(pl find below)
    2-Its not going inside 1st  objClientCtx.executeQueryAsync and just throwing error and typing message which I wrote in failed state "external execute error"
    3-If I am putting filePath  in browser its downloading file with o size. 

    <script type="text/javascript">
    var eventID;
    var objClientCtx
    jquery1_11_1(document).ready(function(){
    
            eventID = 8
    
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){fnGetData();}); 
    
    });
    
    
    function fnGetData() {
    debugger;
        var filePath = 'https://intranet.test.zurichna.com/poc/DS/orc/Lists/Operations%20Request%20Task%20List/'+eventID +'_.000'
    console.log(filePath)
            objClientCtx= new SP.ClientContext.get_current();
            var  web = objClientCtx.get_web();
                       
    var listItemInfo = web.getFileByServerRelativeUrl(filePath)
                    
    var listItemFields = listItemInfo.get_listItemAllFields()
                    
    objClientCtx.load(web);
                    
    objClientCtx.load(listItemInfo);
                    
    objClientCtx.load(listItemFields);
                    
    
    
          
          objClientCtx.executeQueryAsync(
    function (sender, args) {
    var fileVersions = listItemInfo.get_versions();
    objClientCtx.load(fileVersions);
    objClientCtx.executeQueryAsync(
    function (sender, args) {
    debugger;
    var objlistVersionEnumerator = fileVersions.getEnumerator();
    while (objlistVersionEnumerator.moveNext()) {
    var objCurrentListItemVersion = objlistVersionEnumerator.get_current();
    console.log(objCurrentListItemVersion.get_url());
    }
    
    },
    function (sender, args) {console.log('internal execute error');}
    )
    
    }, 
    function (sender, args){
    console.log('external execute error')
    }); 
    
    
    }
    
    </script>




    • Edited by MS_21212 Thursday, December 13, 2018 9:13 AM
    Thursday, December 13, 2018 9:03 AM