Sharepoint 2013 Javascript ClientPeoplePicker default value text

时间:2016-04-04 18:04:50

标签: javascript jquery sharepoint

I'm writing a form in sharepoint 2013 that captures the form via javascript and submits. Everything works, for the most part. However with ClientPeoplePicker, when I get the value, it returns the "Enter a name or email" line as well as my value. Easy enough to parse out, but still annoying as I feel like it shouldn't work that way.

Sadly I am not getting the value in the way that everyone else online is, because "ClientPeoplePicker" never shows up in my rendered code id. I have no idea why. So I am forced to get it by title only.

function formatUserArrayForSharePoint(selector) {
  var users = [];
  $.each(selector, function (i, v) {
    var html = $.parseHTML(v.outerHTML);
    var username = '';
    if ($(html[0]).text().indexOf('Enter a name') !== -1)
      username = $(html[0]).text(0.substring(32, $(html[0]).text().length - 1);
    else username = $(html[0]).text();
    users.push(SP.FieldUserValue.fromUser(username));
  });
  return users;
}

//fill the variable to add to list
field1 = formatUserArrayForSharePoint($("div[title='ascpp']));
oListItem.set_item('field1', field1);
//etc
<SharePoint:ClientPeoplePicker
  AllowMultipleEntities="false"
  runat="server" Title="ascpp"
  ID="field1" AllowEmailAddresses="True" />

Like I said before, when my code renders, there is no format of the id like other examples are.

Example of what others use:

var pickerDiv = $("[id$='ClientPeoplePicker'][title='" + dispTitle + "']");

Example of my rendered code (not entire thing):

<div id="ct100_PlaceHolderMain_field1_TopSpan" class="sp-peoplepicker-topLevel" title="ascpp" spclientpeoplepicker="true">
  <!-- other stuff that is too much to type -->
</div>

2 个答案:

答案 0 :(得分:0)

似乎答案是将不需要的字符剥离。去团队。

答案 1 :(得分:0)

您需要获取SharePoint Client People Picker更改事件的选择器ID。使用id的方法如下。在这里,要获取选择器div,我遵循了通过人员选择器文本框title name$(document).ready进行获取的方法,可以通过检查元素来获取它。将以下代码放入SP.SOD.executeFunc('clientpeoplepicker.js', 'SPClientPeoplePicker', function() { var pickerDiv = $("[id^='Employee_x0020_Name'][title='Employee Name']"); var picker = SPClientPeoplePicker.SPClientPeoplePickerDict[pickerDiv[0].id]; picker.OnUserResolvedClientScript = function(peoplePickerId, selectedUsersInfo) { //It will get the desired display name of the people from picker div, similarly any other property can be accessed via selectedUsersInfo var empname = selectedUsersInfo[0].DisplayText; console.log(empname); } }); 函数中。快乐编码

{{1}}