Skip to content

Instantly share code, notes, and snippets.

@RainerAtSpirit
Created August 26, 2012 11:23
Show Gist options
  • Save RainerAtSpirit/3477704 to your computer and use it in GitHub Desktop.
Save RainerAtSpirit/3477704 to your computer and use it in GitHub Desktop.
Napa OOTB Apps.js leaking globals
var context;
var web;
var user;
// This code runs when the DOM is ready. It ensures the SharePoint
// script file sp.js is loaded and then executes sharePointReady()
$(document).ready(function () {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
});
// This function creates a context object which is needed to use the SharePoint object model
function sharePointReady() {
context = new SP.ClientContext.get_current();
web = context.get_web();
getUserName();
}
// This function prepares, loads, and then executes a SharePoint query to get the current users information
function getUserName() {
user = web.get_currentUser();
context.load(user);
context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
}
// This function is executed if the above call is successful
// It replaces the contents of the 'helloString' element with the user name
function onGetUserNameSuccess() {
$('#message').text('Hello ' + user.get_title());
}
// This function is executed if the above call fails
function onGetUserNameFail(sender, args) {
alert('Failed to get user name. Error:' + args.get_message());
}
// This code runs when the DOM is ready. It ensures the SharePoint
// script file sp.js is loaded and then executes sharePointReady()
$(document).ready(function () {
var context;
var web;
var user;
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
// This function creates a context object which is needed to use the SharePoint object model
function sharePointReady() {
context = new SP.ClientContext.get_current();
web = context.get_web();
getUserName();
}
// This function prepares, loads, and then executes a SharePoint query to get the current users information
function getUserName() {
user = web.get_currentUser();
context.load(user);
context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
}
// This function is executed if the above call is successful
// It replaces the contents of the 'helloString' element with the user name
function onGetUserNameSuccess() {
$('#message').text('Hello ' + user.get_title());
}
// This function is executed if the above call fails
function onGetUserNameFail(sender, args) {
alert('Failed to get user name. Error:' + args.get_message());
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment