Shail S

Shail S

  • NA
  • 455
  • 76.2k

Sharepoint Online- New List Form(Modern/New experience)

May 29 2018 7:38 AM
Hi,
 
I have a custom list created in sharepoint online. I am using the new experience for the list forms. I would want to auto-populate the current user's name in one of the people picker controls on the newlist form. The script i am using works fine in the newlist form when i use it in the classic mode. I have used a script editor to run the js which populates the current user.
 
How can i use this script in the newform.aspx for new/modern experience mode?
 
 
The js code to get the current user-
  1. $(document).ready(function () {  
  2. $("input[title='RequestDays']").attr("readonly","true").css('background-color','#F6F6F6');  
  3. var userid = _spPageContextInfo.userId;  
  4. function GetCurrentUser() {  
  5. //var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";  
  6. var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties/";  
  7. var requestHeaders = { "accept" : "application/json;odata=verbose" };  
  8. $.ajax({  
  9. url : requestUri,  
  10. contentType : "application/json;odata=verbose",  
  11. headers : requestHeaders,  
  12. success : onSuccess,  
  13. error : onError  
  14. });  
  15. }  
  16. function onSuccess(data, request){  
  17. //var loginName = data.d.Title;  
  18. console.log(data);  
  19. var loginName = data.d.DisplayName;  
  20. var myManager = data.d.UserProfileProperties.results[15].Value;  
  21. SetUserFieldValue("Requestor",loginName);  
  22. SetUserFieldValue("Approver",myManager);  
  23. }  
  24. function onError(error) {  
  25. //alert(error);  
  26. }  
  27. function SetUserFieldValue(fieldName, userName) {  
  28. var _PeoplePicker = $("div[title='" + fieldName + "']");  
  29. var _PeoplePickerTopId = _PeoplePicker.attr('id');  
  30. var _PeoplePickerEditer = $("input[title='" + fieldName + "']");  
  31. _PeoplePickerEditer.val(userName);  
  32. var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];  
  33. _PeoplePickerOject.AddUnresolvedUserFromEditor(true);  
  34. }  
  35. GetCurrentUser();  
  36. });  
How am i supposed to inject this js into the newform.aspx for the list?
 
Please advice...

Answers (10)