locked
Get UserID from PeoplePicker RRS feed

  • Question

  • I have a single person people picker column in a list. While creating a listitem, a person is added to that column. In the editform. I would like to get the userid of that person. I tried...

    JSON.parse($('input:hidden[id^="ResponsiblePerson"]').val())[0].EntityData.SPUserID;
    
    $.parse($('input:hidden[id^="ResponsiblePerson"]').val())[0].EntityData.SPUserID;


    Both of these work in IE11 and Firefox. But none of these work in IE9. In IE11, when i switched to IE9 emulation mode, I can see that the following gives null in both document.ready and window.load. So how do I get the user id of the person in the people picker in IE9 and above?

          alert($('input:hidden[id^="ResponsiblePerson"]').attr("value"));


    • Edited by sanjuv Tuesday, January 19, 2016 12:10 PM
    Tuesday, January 19, 2016 6:22 AM

Answers

  • Hi sanjuv,

    Seems that there is an asynchronous method being triggered after the page loaded to retrieve the user information and fill into the hidden input field.

    A workaround is that, you can set a timer to loop check the hidden input field value in code to solve this issue.

    A code demo for your reference:

    <script type="text/javascript">
    
    _spBodyOnLoadFunctionNames.push("ready0");
    
    var intv;
    function ready0()
    {
    	intv = setInterval(ready, 10);
    }
    
    
    function ready()
    {
    	if(undefined !== document.querySelector('input[id^="PG"]'))
    	{
    		if("" !== document.querySelector('input[id^="PG"]').value)
    		{
    			var va = document.querySelector('input[id^="PG"]').value;
    			var val = JSON.parse(va);
    			var id = val[0].EntityData.SPUserID;
    			console.log(id);
    			
    			clearInterval(intv);
    		}
    	}
    }
    
    </script>
    
    

    Best regards,

    Patrick

    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Proposed as answer by sadomovalexMVP Wednesday, January 20, 2016 2:05 PM
    • Marked as answer by Dennis Guo Tuesday, February 2, 2016 9:34 AM
    Wednesday, January 20, 2016 9:40 AM

All replies

  • I ended up querying the same listitem through rest api in document.ready and selecting only the userid and then put all the remaining code in the success of that.  There are 4 people picker columns in the same form and I had to select all the 4 in the rest api filter selection.But if somebody could provide a pure javascript solution that is browser compatible it would be better :(
    • Edited by sanjuv Tuesday, January 19, 2016 11:51 AM
    Tuesday, January 19, 2016 11:49 AM
  • did you try to trace it in jQuery ready function?

    jQuery(function(){
        console.log(jQuery('input:hidden[id^="ResponsiblePerson"]').val());
    });

    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Tuesday, January 19, 2016 2:17 PM
  • Hi sadomovalex

    Thanks for the reply

    Tried that now. Neither in IE10 nor in IE9 it logs anything. But after the page has loaded, if you go to the console and enter it manually, it logs the same correctly.

    Wednesday, January 20, 2016 5:05 AM
  • Hi sanjuv,

    Seems that there is an asynchronous method being triggered after the page loaded to retrieve the user information and fill into the hidden input field.

    A workaround is that, you can set a timer to loop check the hidden input field value in code to solve this issue.

    A code demo for your reference:

    <script type="text/javascript">
    
    _spBodyOnLoadFunctionNames.push("ready0");
    
    var intv;
    function ready0()
    {
    	intv = setInterval(ready, 10);
    }
    
    
    function ready()
    {
    	if(undefined !== document.querySelector('input[id^="PG"]'))
    	{
    		if("" !== document.querySelector('input[id^="PG"]').value)
    		{
    			var va = document.querySelector('input[id^="PG"]').value;
    			var val = JSON.parse(va);
    			var id = val[0].EntityData.SPUserID;
    			console.log(id);
    			
    			clearInterval(intv);
    		}
    	}
    }
    
    </script>
    
    

    Best regards,

    Patrick

    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Proposed as answer by sadomovalexMVP Wednesday, January 20, 2016 2:05 PM
    • Marked as answer by Dennis Guo Tuesday, February 2, 2016 9:34 AM
    Wednesday, January 20, 2016 9:40 AM