This is the closest I've found to returning a variable reference for the logged-on user.
- Try it out it's simple to test:
Try it out it's simple to test:
- In IE browse to your SharePoint site, open developer tools press (f12)
In IE browse to your SharePoint site, open developer tools press (f12)
- Open the Console press (ctl + 2)
Open the Console press (ctl + 2)
- Switch line-mode (with your cursor in the "Console input" press (ctl + alt + m)
Switch line-mode with your cursor in the "Console input" press (ctl + alt + m)
- Paste all the following contents into the console then press (ctl + enter)
Paste all the following contents into the console then press (ctl + enter)
function iWannaDoSomethingWith(thisUser){ if(thisUser === null){ alert('failed to get user'); } else{ alert('User name:' + thisUser.get_title()
- '\n Login Name:' + thisUser.get_loginName());
}
}
function necessaryEvilToWireItAllTogether(nameOfFuncWeReallyWantToCall){
if (typeof(nameOfFuncWeReallyWantToCall)==="function")
{
var ctx = new SP.ClientContext.get_current();
var oWeb = ctx.get_web();
var currentUser = oWeb.get_currentUser();
currentUser.retrieve();
ctx.load(oWeb);
ctx.executeQueryAsync(
function(){
// successful Async call
nameOfFuncWeReallyWantToCall(currentUser);
},
function(){
// failed Async call
nameOfFuncWeReallyWantToCall(null);
}
);
} } ExecuteOrDelayUntilScriptLoaded(function(){necessaryEvilToWireItAllTogether(iWannaDoSomethingWith);}, "sp.js");
- '\n Login Name:' + thisUser.get_loginName());
}
}
function necessaryEvilToWireItAllTogether(nameOfFuncWeReallyWantToCall){
if (typeof(nameOfFuncWeReallyWantToCall)==="function")
{
var ctx = new SP.ClientContext.get_current();
var oWeb = ctx.get_web();
var currentUser = oWeb.get_currentUser();
currentUser.retrieve();
ctx.load(oWeb);
ctx.executeQueryAsync(
function(){
// successful Async call
nameOfFuncWeReallyWantToCall(currentUser);
},
function(){
// failed Async call
nameOfFuncWeReallyWantToCall(null);
}
);
function iWannaDoSomethingWith(thisUser){ if(thisUser === null){ alert('failed to get user'); } else{ alert('User name:' + thisUser.get_title() + '\n Login Name:' + thisUser.get_loginName()); } } function necessaryEvilToWireItAllTogether(nameOfFuncWeReallyWantToCall){ if (typeof(nameOfFuncWeReallyWantToCall)==="function") { var ctx = new SP.ClientContext.get_current(); var oWeb = ctx.get_web(); var currentUser = oWeb.get_currentUser(); currentUser.retrieve(); ctx.load(oWeb); ctx.executeQueryAsync( function(){ // successful Async call nameOfFuncWeReallyWantToCall(currentUser); }, function(){ // failed Async call nameOfFuncWeReallyWantToCall(null); } ); } } ExecuteOrDelayUntilScriptLoaded(function(){necessaryEvilToWireItAllTogether(iWannaDoSomethingWith);}, "sp.js");