1. Retract solution
2. End task "vssphost5.exe" in Task Manager
3. Restart SharePoint Timer Service
4. Deploy solution
Wednesday, September 27, 2017
Tuesday, June 6, 2017
How to make executeQueryAsync() behave synchronously?
You can use JavaScript call backs or Promises/deferreds to work with JavaScript client object model
CallBack Example:
CallBack Example:
$(document).ready(function () {
//don't exectute any jsom until sp.js file has loaded.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', prepareTables);
});
function prepareTables() {
getItemsWithCaml('External User Account Request',
function (camlItems) {
var listItemEnumerator = camlItems.getEnumerator();
while (listItemEnumerator.moveNext()) {
var listItem = listItemEnumerator.get_current();
console.log(listItem.get_item('Title'));
} console.log('Completed table prparation.');
},
function (sender, args) {
console.log('An error occured while retrieving list items:' + args.get_message());
});
}
function getItemsWithCaml(listTitle, success, error) {
var clientContext = new SP.ClientContext.get_current();
var list = clientContext.get_web().get_lists().getByTitle(listTitle);
var camlQuery = new SP.CamlQuery();
var camlItems = list.getItems(camlQuery);
clientContext.load(camlItems);
clientContext.executeQueryAsync(
function () {
success(camlItems);
},
error
);
};
Promises Example:
$(document).ready(function () { //don't exectute any jsom until sp.js file has loaded. SP.SOD.executeFunc('sp.js', 'SP.ClientContext', prepareTables); }); function prepareTables() { getItemsWithCaml('External User Account Request').then( function (camlItems) { var listItemEnumerator = camlItems.getEnumerator(); while (listItemEnumerator.moveNext()) { var listItem = listItemEnumerator.get_current(); console.log(listItem.get_item('Title')); } console.log('Completed table prparation.'); }, function (sender, args) { console.log('An error occured while retrieving list items:' + args.get_message()); } ); } function getItemsWithCaml(listTitle) { //use of $.Deferred in the executeQueryAsync delegate allows the consumer of this method to write 'syncronous like' code var deferred = $.Deferred(); var clientContext = new SP.ClientContext.get_current(); var list = clientContext.get_web().get_lists().getByTitle(listTitle); var camlQuery = new SP.CamlQuery(); var items = list.getItems(camlQuery); clientContext.load(items); clientContext.executeQueryAsync( Function.createDelegate(this, function () { deferred.resolve(items); }), Function.createDelegate(this, function (sender, args) { deferred.reject(sender, args); })); return deferred.promise(); };Origin: http://www.sharepointnadeem.com/2014/10/sharepoint-using-deferredspromises-or.html
Subscribe to:
Posts (Atom)