Last active
August 29, 2015 13:59
-
-
Save iOnline247/10669812 to your computer and use it in GitHub Desktop.
JSOM Paging and BCS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $1_10_2 = jQuery.noConflict(); | |
(function( $, undefined ) { | |
var $formTable = null, | |
results = [], | |
queryPosition = null | |
; | |
// Why wait for the DOM to be ready? | |
ExecuteOrDelayUntilScriptLoaded($.noop, "sp.js"); | |
$(document).ready(function() { | |
$formTable = $(".ms-formtable").eq(0); | |
ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js"); | |
}); | |
function retrieveListItems() { | |
var cachedItems = localStorage.getItem("pl-listItems"); | |
if(cachedItems) { | |
results = JSON.parse(cachedItems); | |
renderTypeAhead(results); | |
} else { | |
queryList(); | |
} | |
} | |
function queryList() { | |
var query = "<View>" + | |
"<Query><Neq><FieldRef Name='VendorID' /><Value Type='Text'>0</Value></Neq></Query>" + | |
"<OrderBy><FieldRef Name='VendorID' Ascending='True' /></OrderBy>" + | |
"<RowLimit>5</RowLimit>" + | |
"<ViewFields><FieldRef Name='BdcIdentity' /><FieldRef Name='Name' /><FieldRef Name='VendorID' /><FieldRef Name='City' /></ViewFields>" + | |
"</View>" | |
; | |
var clientContext = new SP.ClientContext.get_current(); | |
var oList = clientContext.get_web().get_lists().getByTitle("BCS Vendor List"); | |
var camlQuery = new SP.CamlQuery(); | |
camlQuery.set_viewXml(query); | |
camlQuery.set_listItemCollectionPosition(queryPosition); | |
var collListItem = oList.getItems(camlQuery); | |
clientContext.load(collListItem); | |
clientContext.executeQueryAsync( | |
onQuerySucceeded.bind(collListItem), | |
onQueryFailed | |
); | |
} | |
function onQuerySucceeded() { | |
var items = this.get_data(); | |
var item = null; | |
while (item = items.shift()) { | |
item = item.get_fieldValues(); | |
results.push({ | |
"name": item.Name, | |
"vendorId": item.VendorID, | |
"city": item.City, | |
"bdcId": item.BdcIdentity, | |
"value": item.Name, | |
"tokens": [ item.Name, item.VendorID ] | |
}); | |
} | |
// console.log(results.length); | |
queryPosition = this.get_listItemCollectionPosition() && this.get_listItemCollectionPosition().get_pagingInfo(); | |
if(queryPosition) { | |
queryList(); | |
} else { | |
localStorage.setItem("pl-listItems", JSON.stringify(results)); | |
renderTypeAhead(results); | |
} | |
} | |
function onQueryFailed(sender, args) { | |
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); | |
} | |
}( $1_10_2 )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment