User Profile Service in SharePoint 2013

Welcome to a new article of User profile Service in SharePoint 2013. Today I will be showing you a beautiful way to get Current logged in User’s details on a page using javaScript.

So let’s do it,

  • Create a new Page
  • Add a Content Editor web part
  • Place the below code in it.

Code

-----The following HTML code is used to display the Current logged in User name on a Content Editor Web Part.

Name- <span id="username"></span><br/>

---- The following code calls the sp service and append the HTML code replacing by the value.  

  1. <script type="text/javascript">   
  2.   var thisUserAccount ;           
  3.     $(document).ready(function() {  
  4.     thisUserAccount= $().SPServices.SPGetCurrentUser({  
  5.     fieldNames: ["Title","Department","Email"],  
  6.     debug: false  
  7. });  
  8.    
  9. var nametag="<span id=\"username\">"+thisUserAccount.Title+"</span>";  
  10.    
  11. $('#username').replaceWith(nametag);  
  12.    
  13. }     
  14.     ); </script> 

This is the result on the Home page



As you can see name coming up on the page load of the Site similar way you can have the following attributes like Department, Email etc. to your Page.



You just have to make a change like

  • thisUserAccount.Title
  • thisUserAccount.Email
  • thisUserAccount.Department

In the code and hence the purpose will be served.

Till then Keep Learning.

Cheers.