Read Current User with JavaScript and use in Button

Grab the currently Logged In User Using JavaScript

The Javascript:

Place this inside this section: <asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">

<script type="text/ecmascript">
    var context = null;  
    var web = null;  
    var currentUser = null;  
    var currentUserId=''
    
function getUserData()
{
context = new SP.ClientContext.get_current();  
web = context.get_web();  
currentUser = web.get_currentUser();  
currentUser.retrieve();  
context.load(web); 
context.executeQueryAsync(onSuccessMethod, onFailureMethod);   
    }  
        
    function onSuccessMethod()
    {
var userObject = web.get_currentUser();
var loginName = userObject.get_loginName();
var userAccount = loginName.replace("DOMAIN\\","");
var Url = ("https://[somesite.com]?sso_key=[somekey]&user_name=" + userAccount + "@domain.com");
window.open(Url);
    }

    function onFailureMethod(sender, args)
    {
         alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>


Note: The output of this function is to open a new window with the URL built using the current user's account name.  It was used to satisfy a single-sign-on requirement.  It could be modified to perform other actions.

The page html for the button to fire the function and open the window:

<input type="button" value="Test Button" onclick="getUserData()"/>

Comments

Popular posts from this blog

SharePoint Designer 2013 Approval Workflow with Comments

Change SharePoint server hostname and Web Application Names

SharePoint Search - Content Processing Pipeline Failed to Process the Item