2

I am trying to get all versions from every item in a SharePoint list using only client-side through a web part.

Following the exemple at http://techfindings-prem.blogspot.ch/2014/06/how-to-get-all-versions-of-list-using.html and using the "deferred/promise" pattern, I'm stuck at the point of retrieving the fields using the two methods

var listItemInfo = web.getFileByServerRelativeUrl(filePath)   
var listItemFields = listItemInfo.get_listItemAllFields();

The async query keeps failing while my filepath var seems correct (I can use it manually via a browser and correctly get a link to my list's items via their IDs)

Can anyone direct me to what might cause the issue ?

Here is a more extensive piece of the code (the parameters objlistEnumerator and objListItem are correctly extracted, you can get the full web part script at http://www.filedropper.com/historiquewebpart if it helps grabbing the context:

function itemsFieldsListSuccessCallback() {
    alert("p2 itemsFieldsListSuccessCallback");
    this.d.resolve(this.listItemFields);
}

function itemsFieldsListFailCallback() {
    alert("p2 itemsFieldsListFailCallback");
    this.d.reject("something bad happened in itemsFieldsList");
}

function getItemsFieldsList(objlistEnumerator, objListItem) {
    var d = $.Deferred();
    var objListItem = objlistEnumerator.get_current();
    var id = objListItem.get_item('ID');
    var filePath = 'my site collection/Lists/mylist/' + id + '_.000';
    var listItemInfo = web.getFileByServerRelativeUrl(filePath)
    alert(listItemInfo);
    var listItemFields = listItemInfo.get_listItemAllFields();
    alert(listItemFields);
    clientContext.load(web);
    clientContext.load(listItemInfo);
    clientContext.load(listItemFields);
    var o = { d: d, listItemInfo: listItemInfo, listItemFields: listItemFields };
    clientContext.executeQueryAsync(Function.createDelegate(o, itemsFieldsListSuccessCallback), Function.createDelegate(o, itemsFieldsListFailCallback));
    return d.promise();
}

Thanks,

  • You want to get all versions of each item in a list, right? Correct me if I have got wrong.. – Asad Refai May 1 '15 at 18:10
1

Before getting to the cause of your issue, let me give you some recommendations for improving your code, because, as it stands, it is hardly readable.

  1. Avoid clattering the global scope with unnecessary variables and functions. Look into the module pattern, it will help you keep your code cleaner.

  2. You don't really need to use the Function.CreateDelegate, especially considering how you are using it. Instead of this


clientContext.executeQueryAsync(Function.createDelegate(o, itemsFieldsListSuccessCallback), Function.createDelegate(o, itemsFieldsListFailCallback));

you can use this:

 clientContext.executeQueryAsync(
//success
function(){
    deferred.resolve(theValueToReturn);
},   
//failure
function(sender,args){
   deferred.reject(sender, args); 
}

and create a chain of promises, which will be way more readable and expressive. Eg.

getList().then(getListItems).then(getAllFields).then...
  1. Never supress or discard the failure return values. executeQueryAsync, in case of failure, will pass two values to your callback function, sender and args. You may want to look into args.get_message() to understand what the actual error was.

Now, as for your code, I can spot two major issues:

  1. var filePath = 'my site collection/Lists/mylist/' + id + '_.000'; var listItemInfo = web.getFileByServerRelativeUrl(filePath)

Make extra sure that your filePath is correct.

  1. I downloaded your code, and found this function

    getItemsFieldsVersionsList(listItemFields) { //... 
    var fileVersions = listItemFields.get_versions();
    }
    

    You should call get_versions() on the listItemInfo object instead.

    var fileVersions = listItemInfo.get_versions(); ctx.load(fileVersions)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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