2

I have a form where I used a People Picker control. I would like to get the username entered into a people picker control using jQuery.

Please suggest?

  • Please tag by feature or topic and not by product (or version). This helps to attract more attention to your question and keep it relevant. See How do I use tags for general guidelines. – Alex Angas Sep 18 '11 at 2:04
  • 2
    Would you consider accepting an answer? It's important for community so when an answer is searched, one knows that it has an useful answer. – Janis Veinbergs Sep 22 '11 at 10:36
5

To leverage your work, you can use SharePoint jQuery based library to work with your forms and fields: jPoint - jQuery for SharePoint forms, lists, webparts, and webservices

And then you just do: jP.Form.readForm().MyUserField.val();

4

Here is a plain jQuery example. I can't take credit for it, I believe Marc Anderson came up with it. Just change the column variable with the display name of your column.

var columnName = 'Assignee';
   // 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).closest("td.ms-formbody").delegate("a", "click", function() {
               alert($(this).closest("td.ms-formbody").find("div[id*='divEntityData']").attr("description"));
         });
      } //if.searchText
   }); //td.ms-formbody.each
2

unfortunately, you can't just check for the .value of a "People Picker" field, you have to test the textarea to see if something is in there and/or if the validated name is in the associated div. The following function will return true if the people picker is valid...

note the 'requireValid' parameter, if this is false, it will return true if just something is typed into the people picker... if this parameter is true, it will require that the name is also validated.

function IsPeoplePickerPopulated(MyFieldName,requireValid){
  var ret = false;
  $("textarea[title='People Picker']").each(function(){
    var fieldName = $(this).closest("td.ms-formbody").prev().find("H3").text();
    if(fieldName == MyFieldName){
      var inputText = $(this).val();
      var validatedName = $(this).closest("td.ms-formbody").find("div.ms-inputuserfield").text();
      if(inputText != "" && !requireValid){
        ret = true;
      }
      else if(validatedName != "" && requireValid){
        ret = true;
      }
    }
  });
  return ret;
}

if(IsPeoplePickerPopulated('Owner',true)){
  alert("The Owner is validated!")
}
else{
  alert("Please insert a valid identity into the Owner field!");
}

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.