none
SharePoint 2013 List & Infopath form autofill CurrentName Javascript jquery RRS feed

  • Question

  • hi

    Field name:  user - single line of text.  To autofill the current user name to the Sharepoint field "User" in javascrpt or jquery?


    thank you!


    Thursday, November 14, 2019 1:50 AM

All replies

  • Hi Irene,

    If you want to use JavaScript code to achieve it, the following code for your reference.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
    $(function(){
    	var userid=_spPageContextInfo.userId;
    	var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
    	$.ajax({
    		 url: requestUri,
    		 method: "GET",
    		 async:false,
    		 headers: { "Accept": "application/json; odata=verbose" },
    		 success: function (data) {
    			$("td>h4>span:contains('User')").closest("tr").find("input").val(data.d.Title);			
    		 },
    		 error: function (data) {
    		 }
    	 });
    });
    </script>

    We can also use no code solution to achieve it. Getting current user name from user information list or using UserProfileService.asmx, the following articles for your reference.

    How to automatically retrieve current UserName in an InfoPath SharePoint list form

    InfoPath – User Information List

    Best regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, November 14, 2019 7:04 AM
    Moderator
  • hi Dennis

    No, it didn't work.

    I added in the script editor - It didn't flow the User text box.

    I added the Data source soap service - userprofileservice, and I received a warning error everything when I open the form

    Warning
    An error occurred querying a data source

    thank you!


    • Edited by Irene Lui Thursday, November 14, 2019 11:31 PM
    Thursday, November 14, 2019 9:09 PM
  • Hi Irene,

    Please provide some screenshots about the Infopath form for further research.

    In my test, the form like below.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Friday, November 15, 2019 1:19 AM
    Moderator
  • hi Dennis

    I got it to work.  I figure it out.

    thank you!

    Irene

    Friday, November 15, 2019 2:30 AM
  • Hi Irene,

    Can you share the solution? It will help others who suck with the problem!

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, November 15, 2019 6:25 AM
    Moderator