1

I am using SP 2013 JSOM for paging and sorting the list data in <table>. I have referred the code from this link https://code.msdn.microsoft.com/sharepoint/SharePoint-JSOM-list-5104ca92/sourcecode?fileId=92627&pathId=789885789
The problem is when I am using sortColumn as Created and click on the Previous Page button (btnBack) of paging, then it throws error Request Failed.Exception from HRESULT: 0x80131904 undefined
But for any other column it works fine.
Is it because it is Date/Time column ?

var context, 
    web, 
    spItems, 
    position, 
    nextPagingInfo, 
    previousPagingInfo, 
    listName = 'ContactsList', 
    pageIndex = 1, // default page index value 
    pageSize = 4, // default page size value 
    list, 
    camlQuery, 
    sortColumn = 'Created'; // this is sort column, you can add more than one column, but you should add it also to CAML Query & managePagerControl function 

// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model  
$(document).ready(function () { 
    context = SP.ClientContext.get_current(); 
    list = context.get_web().get_lists().getByTitle(listName); 
    camlQuery = new SP.CamlQuery(); 

    $("#btnNext").click(function () { 
        pageIndex = pageIndex + 1; 
        if (nextPagingInfo) { 
            position = new SP.ListItemCollectionPosition(); 
            position.set_pagingInfo(nextPagingInfo); 
        } 
        else { 
            position = null; 
        } 

        GetListItems(); 
    }); 

    $("#btnBack").click(function () { 
        pageIndex = pageIndex - 1; 
        position = new SP.ListItemCollectionPosition(); 
        position.set_pagingInfo(previousPagingInfo); 
        GetListItems(); 
    }); 

    GetListItems(); 
}); 

function GetListItems() { 
    //Set the next or back list items collection position 
    //First time the position will be null 
    camlQuery.set_listItemCollectionPosition(position); 

    // Create a CAML view that retrieves all contacts items  with assigne RowLimit value to the query 
    camlQuery.set_viewXml("<View>" + 
                                "<ViewFields>" + 
                                       "<FieldRef Name='FirstName'/>" + 
                                       "<FieldRef Name='Title'/>" + 
                                       "<FieldRef Name='Company'/>" + 
                                  "</ViewFields>" + 
                               "<Query>" + 
                                    "<OrderBy>" + 
                                      "<FieldRef Name='" + sortColumn + "' Ascending='true' />" + 
                                    "</OrderBy>" + 
                               "</Query>" + 
                               "<RowLimit>" + pageSize + "</RowLimit></View>"); 

    spItems = list.getItems(camlQuery); 

    context.load(spItems); 
    context.executeQueryAsync( 
            Function.createDelegate(this, onSuccess), 
            Function.createDelegate(this, onFail) 
        ); 
} 

// This function is executed if the above OM call is successful 
// This function render the returns items to html table 
function onSuccess() { 

    var listEnumerator = spItems.getEnumerator(); 
    var items = []; 
    var item; 

    while (listEnumerator.moveNext()) { 
        item = listEnumerator.get_current(); 
        items.push("<td>" + item.get_item('FirstName') + "</td><td>" + item.get_item('Title') + "</td><td>" + item.get_item('Company') + "</td>"); 
    } 

    var content = "<table><tr><th>First Name</th><th>Last Name</th><th>Company</th></tr><tr>" 
                + items.join("</tr><tr>") + "</tr></table>"; 
    $('#content').html(content); 

    managePagerControl(); 
} 

function managePagerControl() { 

    if (spItems.get_listItemCollectionPosition()) { 
        nextPagingInfo = spItems.get_listItemCollectionPosition().get_pagingInfo(); 
    } else { 
        nextPagingInfo = null; 
    } 

    $("#pageInfo").html((((pageIndex - 1) * pageSize) + 1) + " - " + ((pageIndex * pageSize) - (pageSize - spItems.get_count()))); 

    previousPagingInfo = "PagedPrev=TRUE&Paged=TRUE&p_ID=" + spItems.itemAt(0).get_item('ID') + "&p_" + sortColumn + "=" + encodeURIComponent(spItems.itemAt(0).get_item(sortColumn)); 

    if (pageIndex <= 1) { 
        $("#btnBack").attr('disabled', 'disabled'); 
    } 
    else { 
        $("#btnBack").removeAttr('disabled'); 
    } 

    if (nextPagingInfo) { 
        $("#btnNext").removeAttr('disabled'); 
    } 
    else { 
        $("#btnNext").attr('disabled', 'disabled'); 
    } 

} 

// This function is executed if the above call fails 
function onFail(sender, args) { 
    alert('Failed to get items. Error:' + args.get_message()); 
}
3

"Created" returns a Date Object, you cannot use it for sorting. It adds [Object Object] into your pagingInfo string, Use "Created_x0020_Date" instead, it returns string and works perfectly, I ran into the same problem and using "Created_x0020_Date" fixed the issue

sortColumn = 'Created_x0020_Date';

or debug the JS, and see how OOTB .get_pagingInfo() function returning the value of Date Object and convert the date to that string format (.toISOString() in your case) and append to prevPagingInfo

  • Is this a custom column(calculated) or default because when I used it, the code throws error : Field with name "Created_x0020_Date" was not found – Yayati Aug 19 '15 at 13:08
  • It is an OOTB column, I wonder why it is not on your list, It may have different name on your list. – sekhar Aug 19 '15 at 13:53
  • you can format the date value to SharePoint date format, I'm not sure what the format is, but the actual problem is you are trying to add [Object] to String, So try to format the date value, Or use any other calculated column / Modified – sekhar Aug 19 '15 at 14:52
  • or you can debug how the OOTB get_nextPaginInfo() value returned and form prevPaginInfo it in the same way. – sekhar Aug 19 '15 at 15:01
  • 1
    I converted the Created date object to ISO string using item.get_item('Created'). toISOString() and it worked fine – Yayati Aug 19 '15 at 15:14

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.