Unable to get SPCurrentUser to work to pre-populate peoplePicker ctrl

Feb 14, 2012 at 7:00 PM
Edited Feb 14, 2012 at 7:28 PM

Trying to get the "Assigned To" (a peoplepicker) on a SharePoint NewItem form to pre-populate with the current user.
I used selectorgadget to obtain the following id of my formfield- #ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField_downlevelTextBox.

I also have studied Marc Anderson's blog posts, demo, Dave Cavins' blog, as well as some of the documentation for the SPServices jQuery library for SharePoint. I adapted the following script to fit my needs

<script type="text/javascript" src="/jquery/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/jquery/jquery.SPServices-0.7.0.min.js"></script>
<script type="text/javascript"> 
   $(document).ready(function() { 
      $('textarea#ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField_downlevelTextBox').text(
         $().SPServices.SPGetCurrentUser({
            fieldName: "Assigned To",debug: false})
      );
   }); 
</script>

However, it is not working for me. The "Assigned To" field is blank when NewForm loads, and still blank after I save the record. Can anyone tell me what I'm doing incorrectly?

Many Thanks
Jg

UPDATE1: the method .text() to set the CurrentUser seems a little suspect to me, as you are actually telling the people picker to look for the AD_object, so I tested .val() instead, however that didn't work either.

Coordinator
Feb 14, 2012 at 7:27 PM

Jg:

In my post that you link to above, you'll see that I'm seeting the value of a div, not a textarea:

var columnName = 'yourcolumnname';
var userName = 'domain\\username';
// There's no easy way to find one of these columns; we'll look for the comment with the columnName
var searchText = RegExp("FieldName=\"" + columnName + "\"", "gi");
// Loop through all of the ms-formbody table cells
$("td.ms-formbody").each(function() {
  // Check for the right comment
  if(searchText.test($(this).html())) {
    $(this).find("div[Title='People Picker']").html(userName);
    return false;
  }
});

The People Picker is a compound control made up of multiple HTML elements. Because SharePoint runs some script under the covers, you need to set the right value on the right element to make things work.

M.

Feb 15, 2012 at 5:34 PM
Edited Feb 15, 2012 at 6:19 PM

Marc,

I've been able to isolate the following code on the src that is responsible rendering the cell with the people picker control in my SP form:

  

    <td width="400px" valign="top" class="ms-formbody"><span dir="none">
       <input name="ctl00$m$g_7eb2db37_cd89_4064_bb9d_3b85682bc642$ff6_1$ctl00$ctl00$HiddenUserFieldValue" type="hidden" id="ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_HiddenUserFieldValue" />
       <span id="ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField" class="ms-usereditor" NoMatchesText="&lt;No Matching Names>" MoreItemsText="More Names..." RemoveText="Remove" value="" allowEmpty="1" ShowEntityDisplayTextInTextBox="0" EEAfterCallbackClientScript="">
	  <input name="ctl00$m$g_7eb2db37_cd89_4064_bb9d_3b85682bc642$ff6_1$ctl00$ctl00$UserField$hiddenSpanData" type="hidden" id="ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField_hiddenSpanData" />
	  <input name="ctl00$m$g_7eb2db37_cd89_4064_bb9d_3b85682bc642$ff6_1$ctl00$ctl00$UserField$OriginalEntities" type="hidden" id="ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField_OriginalEntities" value="&lt;Entities />" />
	  <input name="ctl00$m$g_7eb2db37_cd89_4064_bb9d_3b85682bc642$ff6_1$ctl00$ctl00$UserField$HiddenEntityKey" type="hidden" id="ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField_HiddenEntityKey" />
	  <input name="ctl00$m$g_7eb2db37_cd89_4064_bb9d_3b85682bc642$ff6_1$ctl00$ctl00$UserField$HiddenEntityDisplayText" type="hidden" id="ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField_HiddenEntityDisplayText" />
	     <table id="ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField_OuterTable" class="ms-usereditor" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;">
		<tr valign="bottom">
		   <td valign="top" style="width:90%;">
		      <table cellpadding="0" cellspacing="0" border="0" style="width:100%;table-layout:fixed;">
			 <tr>
			    <td>
			       <div id="ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField_upLevelDiv" name="upLevelDiv" class="ms-inputuserfield" TabIndex="0" style="display: none;position: absolute; " AutoPostBack="0">
			       </div>
			       <textarea name="ctl00$m$g_7eb2db37_cd89_4064_bb9d_3b85682bc642$ff6_1$ctl00$ctl00$UserField$downlevelTextBox" rows="1" cols="20" id="ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField_downlevelTextBox" class="ms-input" onKeyDown="return onKeyDownRw(this, 'ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField', 3, true, event);" onKeyUp="onKeyUpRw('ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField');" Title="People Picker" AutoPostBack="0" style="width:100%;wordwrap: break-word;">
			       </textarea>
			    </td>
			 </tr>
		       </table>
		   </td>
		   <td align="right" valign="top" nowrap="true" style="padding-left:5px;">
		      <a id="ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField_checkNames" title="Check Names" onclick="var arg=getUplevel('ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField');var ctx='ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField';EntityEditorSetWaitCursor(ctx);WebForm_DoCallback('ctl00$m$g_7eb2db37_cd89_4064_bb9d_3b85682bc642$ff6_1$ctl00$ctl00$UserField',arg,EntityEditorHandleCheckNameResult,ctx,EntityEditorHandleCheckNameError,true);return false;" href="javascript:">
			<img title="Check Names" src="/_layouts/images/checknames.gif" alt="Check Names" style="border-width:0px;" />
		      </a>&nbsp;
		      <a id="ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField_browse" accesskey="B" title="Browse" onclick="__Dialog__ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField(); return false;" href="javascript:">
			<img title="Browse" src="/_layouts/images/addressbook.gif" alt="Browse" style="border-width:0px;" />
		      </a>
		   </td>
		</tr>
		<tr>
		   <td colspan="3">
		      <span id="ctl00_m_g_7eb2db37_cd89_4064_bb9d_3b85682bc642_ff6_1_ctl00_ctl00_UserField_errorLabel" class="ms-error">
		      </span>
		   </td>
		</tr>
	     </table>
	</span>
    </span></td>

You suggested that the peoplepicker control is made up of multiple HTML elements, do you suggest I try setting the .text of the input selector?

 Here are some other selectors I've tried-

      $("span[dir='none']").text(

      $('span:contains(_ff6_1)').text( -- neither of them seemed to work :(

Coordinator
Feb 15, 2012 at 6:54 PM

I think you may be looking at View Source rather than the live DOM. What I meant above is that there is script which "runs" the control. Some of the elements in the live DOM are created by that script and are not in the markup as it comes from the server.

var peoplePicker = $("div[Title='People Picker']");
$(peoplePicker).html("DOMAIN\\username");

If you type the script above in the console of your browser with a form loaded that has at least one People Picker in it, you'll see what happens.

M.

Feb 15, 2012 at 7:29 PM

Marc,

I've been sort of hesistant to use the EXACT code you wrote in your post, b/c I'm not quite sure I understand how it works, and also I didn't want to be locked into populating my for a static domain\userName. From what I could interpret, I have made changed my script to the following and tested it, although it is still not populating the people picker as I expect. Could you please take a look? I'd really appreciate it.

<script type="text/javascript" src="/jquery/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/jquery/jquery.SPServices-0.7.0.min.js"></script>
<script type="text/javascript"> 
   $(document).ready(function() { 
      var columnName = 'Assigned To';
      var searchText = RegExp ("FieldName=\"" +columnName+ "\"", "gi");
	 $("td.ms-formbody").each(function() {
	 //check for right comment
	 if(searchText.test($(this).html())) {
	    $(this).find("div[Title='People Picker']").html($().SPServices.SPGetCurrentUser({
		fieldName: "Assigned To",
		debug: false
	    });
	    return false;
      	 }
   }); 
</script>

Thanks! Jg

Feb 15, 2012 at 7:32 PM

Thanks for getting back to me so quickly. I will try to look at the DOM in firebug.

Coordinator
Feb 15, 2012 at 7:43 PM

This piece isn't going to work: 

$(this).find("div[Title='People Picker']").html($().SPServices.SPGetCurrentUser({
  fieldName: "Assigned To",
  debug: false
});

"Assigned To" isn't a value which is available from the SPGetCurrentUser function. I think you want "Name", which will give you the current user's account.

M.

Feb 16, 2012 at 1:27 PM

Marc,

Thanks for all of your assistance! Hopefully your replies can help other users.

I ended up going a different route to get my "assigned to" field to populate in my SharePoint website. I ended up using a workflow to populate the field... which, looking back I now realize probably should have been my approach all along.

Thanks again! Jg