Set Current Logged User Name In SharePoint 2013 People Picker Field Using JavaScript/jQuery And REST

I'm happy to say this is my 200th blog in C-SharpCorner.

Normally, in a list, where we havea  Person/Group column (say Employee), when we use REST API to get this column value, we get User ID instead of an actual value, which we see in list column.

The reason behind this is Person/Group column is actually a lookup field internally.

Steps
  1. Open your Notepad.
  2. Copy the code given below and paste it.
  3. Name it as like spvalidation.js.
  4. Add Content Editor Webpart in your page.
  5. Add saved .js file into your Webpart properties.
Source code 
  1. <script>  
  2. $(document).ready(function () {  
  3.   
  4. GetUserLogin();  
  5.   
  6. });  
  7.   
  8. var userid = _spPageContextInfo.userId;  
  9.   
  10. function GetUserLogin() {  
  11. var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";  
  12.   
  13. var requestHeaders = { "accept" : "application/json;odata=verbose" };  
  14.   
  15. $.ajax({  
  16.   url : requestUri,  
  17.   contentType : "application/json;odata=verbose",  
  18.   headers : requestHeaders,  
  19.   success : QuerySuccess,  
  20.   error : QueryError  
  21. });  
  22. }  
  23.   
  24. function QuerySuccess(data, request){  
  25.     
  26.   var loginName = data.d.LoginName.split('|')[1];  
  27.   $("div[title='Enter the People picker column Name']").val(loginName);  
  28. }  
  29.   
  30. function QueryError(error) {  
  31.   alert(error);  
  32. }  
  33. </script>   
Was my blog helpful? If yes, please let me know and if not, please explain what was confusing or missing.

I’ll use your feedback to double-check the facts, add info, and update this blog.

Thanks to all SharePoint readers.