Created
November 1, 2013 02:57
-
-
Save kitmenke/7260426 to your computer and use it in GitHub Desktop.
An example of getting the current user via SharePoint's client object model
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
<script type="text/javascript"> | |
function CustomExecuteFunction() { | |
var self = this; | |
self.context = new SP.ClientContext.get_current(); | |
self.web = context.get_web(); | |
self.currentUser = web.get_currentUser(); | |
context.load(self.currentUser); | |
self.asyncSuccess = function(sender, args) { | |
var user = this.currentUser; | |
console.log('asyncSuccess', user); | |
document.getElementById('userLoginName').innerHTML = user.get_loginName(); | |
document.getElementById('userId').innerHTML = user.get_id(); | |
document.getElementById('userTitle').innerHTML = user.get_title(); | |
document.getElementById('userEmail').innerHTML = user.get_email(); | |
var url = '?userinfo='; | |
url += '0[' + encodeURIComponent(user.get_loginName()) + ']'; | |
url += '1[' + encodeURIComponent(user.get_id()) + ']'; | |
url += '2[' + encodeURIComponent(user.get_title()) + ']'; | |
url += '3[' + encodeURIComponent(user.get_email()) + ']'; | |
document.getElementById('customButton').href += url; | |
}; | |
self.asyncFailure = function(sender, args) { | |
alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace()); | |
}; | |
// actually fires off the AJAX request | |
// more info: http://msdn.microsoft.com/en-us/library/dn168907.aspx | |
context.executeQueryAsync( | |
Function.createDelegate(self,self.asyncSuccess), | |
Function.createDelegate(self,self.asyncSuccess) | |
); | |
} | |
ExecuteOrDelayUntilScriptLoaded(CustomExecuteFunction,'sp.js'); | |
</script> | |
<div>Current Logged User: | |
<span id="userLoginName"></span> | |
<span id="userId"></span> | |
<span id="userTitle"></span> | |
<span id="userEmail"></span> | |
<a id="customButton" href="https://example.sharepoint.com/SitePages/Test.aspx">Click me!</a> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment