You can use the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 | // Extending JSOM SP.ClientContext.prototype.executeQuery = function (argument) { // Get a reference to this var that = this; // Create the promise var promise = new Promise(function (resolve, reject) { that.executeQueryAsync( function () { resolve(argument); }, function (sender, e) { reject(e); } ); }); return promise; }; |
With that it is now easier to write something as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $(document).ready(function () { getUser().then( function (user, folder) { $('#message').text('Hello ' + user.get_title()); }, function (e) { alert('Failed to get user name. Error:' + e.get_message()); } ) }); // This function prepares, loads, and then executes a SharePoint query to get the current users information function getUser() { var user = context.get_web().get_currentUser(); context.load(user); return context.executeQuery(user); } |
No comments:
Post a Comment