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-
- $(document).ready(function () {
- $("input[title='RequestDays']").attr("readonly","true").css('background-color','#F6F6F6');
- var userid = _spPageContextInfo.userId;
- function GetCurrentUser() {
- //var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
- var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties/";
- var requestHeaders = { "accept" : "application/json;odata=verbose" };
- $.ajax({
- url : requestUri,
- contentType : "application/json;odata=verbose",
- headers : requestHeaders,
- success : onSuccess,
- error : onError
- });
- }
- function onSuccess(data, request){
- //var loginName = data.d.Title;
- console.log(data);
- var loginName = data.d.DisplayName;
- var myManager = data.d.UserProfileProperties.results[15].Value;
- SetUserFieldValue("Requestor",loginName);
- SetUserFieldValue("Approver",myManager);
- }
- function onError(error) {
- //alert(error);
- }
- function SetUserFieldValue(fieldName, userName) {
- var _PeoplePicker = $("div[title='" + fieldName + "']");
- var _PeoplePickerTopId = _PeoplePicker.attr('id');
- var _PeoplePickerEditer = $("input[title='" + fieldName + "']");
- _PeoplePickerEditer.val(userName);
- var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
- _PeoplePickerOject.AddUnresolvedUserFromEditor(true);
- }
- GetCurrentUser();
- });
How am i supposed to inject this js into the newform.aspx for the list?
Please advice...

Nanddeep NachanRank #38 · 35.2K reputationPosted May 29, 2018, 8:56 PM EDT
Chanakya JayabalanRank #1,183 · 1.5K reputationPosted May 30, 2018, 10:59 PM EDT
Chanakya JayabalanRank #1,183 · 1.5K reputationPosted Jun 1, 2018, 9:43 PM EDT
Shail SRank #1,940 · 757 reputationPosted May 29, 2018, 9:03 PM EDT
Nanddeep NachanRank #38 · 35.2K reputationPosted May 29, 2018, 9:07 PM EDT
Shail SRank #1,940 · 757 reputationPosted May 30, 2018, 10:16 PM EDT
Nanddeep NachanRank #38 · 35.2K reputationPosted May 30, 2018, 10:50 PM EDT
Shail SRank #1,940 · 757 reputationPosted May 31, 2018, 12:14 AM EDT
Nanddeep NachanRank #38 · 35.2K reputationPosted May 31, 2018, 12:30 AM EDT
Nanddeep NachanRank #38 · 35.2K reputationPosted May 31, 2018, 12:39 AM EDT