4

I am using new form in sharepoint designer , so how will i autopopulate name picker field(Name of the submitter) to take logged in user name automatically when form is opened?

1
2

For NewForm.aspx one of the best way is to use "spservices.js":

don't forget to add reference of spservice.js after jquey: https://spservices.codeplex.com/releases/view/119578

script for Auto popultation is as under:

$(document).ready(function () {
    var currentUser = $().SPServices.SPGetCurrentUser({
        webURL: _spPageContextInfo.webServerRelativeUrl,
    });
    setCurrentUser(currentUser);
});

function setCurrentUser(accountName) {
    setTimeout(function () {
        var userName = accountName;
        var form = jQuery("table[class='ms-formtable']"); 
        var userField = form.find("input[id$='ClientPeoplePicker_EditorInput']").get(1) 
        var peoplepicker = SPClientPeoplePicker.PickerObjectFromSubElement(userField)
        peoplepicker.AddUserKeys(userName);
    }, 300);
}
5
  • Hi Rushil do i need to change anything in the code ? My field name is Name of the submitter(Name_x0020_of_x0020_the_x0020_su) , i applied your code above but its not working!! Also i used those ref js too! I Am using SP2013 @Rushil Shah
    – OM-ॐ
    Apr 4 '17 at 12:04
  • Make change in the code by replacing below line with corrected one var userField = form.find("input[id$='ClientPeoplePicker_EditorInput']").get(1) to var userField = form.find("input[id$='ClientPeoplePicker_EditorInput']").get(0) Apr 4 '17 at 12:14
  • Do i need to change these - 'ClientPeoplePicker_EditorInput' , userField with my column name? I mentioned the column name can you please tell me how to write there? I am new too javascript
    – OM-ॐ
    Apr 4 '17 at 12:16
  • function setCurrentUser(accountName) { setTimeout(function () { var userName = accountName; var form = jQuery("table[class='ms-formtable']"); var userField = form.find("input[id$='Name_x0020_of_x0020_the_x0020_su']").get‌​(0) var peoplepicker = SPClientPeoplePicker.PickerObjectFromSubElement(Nameofthesubmitter) peoplepicker.AddUserKeys(userName); }, 300); }
    – OM-ॐ
    Apr 4 '17 at 12:22
  • @RushilShah, please edit your answer instead add comments with code. Comments are temporary. Thank you. PS: if you have indeed edit your question with the code, please delete your own coments. Apr 4 '17 at 14:31
2

No need for jQuery or SPServices (they were handy in SP2007/SP2010 days)

3 lines JavaScript:

var inp = document.querySelector("input[title='Assigned To']");
inp.value = _spPageContextInfo.userDisplayName;
SPClientPeoplePicker.SPClientPeoplePickerDict[inp.parentNode.id].AddUnresolvedUserFromEditor(true);

Suggestion from comments: 1 line of code:

SPClientPeoplePicker.SPClientPeoplePickerDict[document.querySelector("input[title='Assigned To']").parentNode.id].BatchAddUserKeysOperation([_spPageContextInfo.userDisplayName], 0);

see: Trigger SPClientPeoplePicker to search for string

4
  • Hey Danny , any extra modification i need to do in this code? My field name is Name of the submitter(Name_x0020_of_x0020_the_x0020_su) and also should i ref some js ? I Am new to javascript please help me out
    – OM-ॐ
    Apr 4 '17 at 12:28
  • Check the HTML source with F12 tools for the correct title= notation, and execute the code after the form is loaded.. the setTimeOut in the other example is a crude way, but works. Best is to use the Cisar Chrome Extension WYSIWIG Javascript editor (use Google!) and add the code to the OnPostRender function Apr 4 '17 at 12:31
  • Be warned .. That OnPostRender functions executes once for every Form Field So you need to add a check to only execute once Apr 4 '17 at 12:36
  • @Danny'365CSI'Engelman Just a minimal improvement - no need for setting the input value picker.BatchAddUserKeysOperation(['userDisplayNameOrLoginOrSearchString'], 0); also does the trick. sharepoint.stackexchange.com/questions/197270/…
    – Mx.
    Apr 4 '17 at 13:57
0

Not sure about SP Designer, but using Infopath you can create a rule on form load to set a field to the current user.

3
  • Can you describe the process for infopath please? @JJD
    – OM-ॐ
    Apr 4 '17 at 10:23
  • 1 Look for the Form load button (it's in the DATA tab in the ribbon.) 2 Make a new rule of type Action, add an action 'Set a fields value'. 3 In the popup menu pick the field. Click the fx button on the Value field, insert a function of category:User, then select UserName and click ok
    – JJD
    Apr 4 '17 at 10:29
  • I tried the exact process but its not working bro!!
    – OM-ॐ
    Apr 4 '17 at 10:41
0

have you try this:

<script type="text/javascript"> 
$(document).ready(function() { 
$('div.ms-inputuserfield').text($().SPServices.SPGetCurrentUser({fieldName: "Title",debug: false}));
</script>

https://social.msdn.microsoft.com/Forums/office/en-US/342ac0a6-0782-40f8-8c6e-d8b57e494b8b/sharepoint-person-field-auto-fill-with-current-user?forum=sharepointdevelopmentprevious

0
function getUser() {
    var userid = _spPageContextInfo.userId;


    var requestUri = "https://brillioonline.sharepoint.com/sites/ConnectDeveloperSite/AxisBilling/axisdev" + "/_api/web/getuserbyid(" + userid + ")";
    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;

       $("#UserID").append("UserID")
      // $("#UserID").val(loginName )

    } function onError(error) {
        alert("Error on retrieving current user.");
    }
}
getUser();

And then we need to append with field value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.