jsomLoginNamepopulate


Title: Get Login ID, Name and display that data in Textbox.

Description:
We have requirement that need to fill the form. To fill the form User ID and User name is a Log in User information. To full fill that requirement we will get the User Id and User name and bind information to the appropriate text boxes.

Steps:
Design Form: 

 
 Expected Out put:  Customer ID and Customer Name data load automatically when open the Form. Remaining text bxes fill Manually based on requirement.
 
 Code:
  

  1.   <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

  2. <script type="text/javascript">   
  3.    
  4.         $(document).ready(function() {    
  5.             var currentUser;    
  6.             if (SP.ClientContext != null) {    
  7.                 SP.SOD.executeOrDelayUntilScriptLoaded(getCurrentUser, 'SP.js');    
  8.             } else {    
  9.                 SP.SOD.executeFunc('sp.js', null, getCurrentUser);    
  10.             }    
  11.         });    
  12.   
  13.   
  14.         function getCurrentUser() {    
  15.             context = new SP.ClientContext.get_current();    
  16.             web = context.get_web();    
  17.             currentUser = web.get_currentUser();    
  18.             context.load(currentUser);    
  19.             context.executeQueryAsync(onSuccess, onRequestFail);    
  20.         }    
  21.         
  22.         function onSuccess(sender, args) {    
  23.                  
  24.                
  25.                 $('#currentUserID').val(currentUser.get_id())  
  26.                 $('#currentUserName').val(currentUser.get_title())  
  27.   
  28.                    
  29.    
  30.         }    
  31.         
  32.         function onRequestFail(sender, args) {    
  33.             alert('request failed' + args.get_message());    
  34.         }    
  35.     </script>   
  36. Customer ID <input type="text" id="currentUserID" /> </br>  
  37. Customer Name <input type="text" id="currentUserName" /> </br>  
  38. Location <input type="text" id="currentUserID" /> </br>  
  39. Address <input type="text" id="currentUserName" />