Thursday, March 29, 2012

People Picker +JQuery

Validate People Picker value with NULL/empty
----------------------------------------------

<script language="javascript" src="URL/Shared%20Documents/jquery-1.9.1.min.js" type="text/javascript"></script><script type="text/javascript">

function getPickerInputElement(pickerNo) {

 var result = "";
 var j = 0;
 var divs = document.getElementsByTagName("DIV");
 for (var i=0; i<divs.length; i++) {
   if (divs[i].id.indexOf("UserField_upLevelDiv") > 0) {
     j++;
     if (j == pickerNo) {
       result = divs[i];
       break;
     }
   }
 }
 return result;
}




function PreSaveAction()
{

   var objPeoplePicker = getPickerInputElement(1);

    var ppValue=objPeoplePicker.innerText;
   if(ppValue.trim().length==0)
   {

          alert('Pleae enter valid email address');
    return false;          

   }
return true;
   }</script>


---------------------------------------------------------------------------------------

Get People Picker Value:
------------------------
function getPickerInputElement(pickerNo) {
 var result = "";
 var j = 0;
 var divs = document.getElementsByTagName("DIV");
 for (var i=0; i<divs.length; i++) {
   if (divs[i].id.indexOf("UserField_upLevelDiv") > 0) {
     j++;
     if (j == pickerNo) {
       result = divs[i];
       break;
     }
   }
 }
 return result;
}

call the above function passing People Picker Number in the form (if you have two people pickers and want to get the value of second picker)

var objPeoplePicker = getPickerInputElement(2);

Assign Empty value:
-----------------------
objPeoplePicker.innerHTML="";



Assign Current User into People Picker:
---------------------------------------------------
$(document).ready(function()
{
var userName = $().SPServices.SPGetCurrentUser({
  fieldName: "Title",debug:false});

$("textarea[title='People Picker']").val(userName);
$("div[title='People Picker']").text(userName);
or

var userName = $().SPServices.SPGetCurrentUser({
  fieldName: "Name",debug:false});

$("textarea[title='People Picker']").val(userName);
$("div[title='People Picker']").text(userName);
});

--------------------------------

For fields:
<script src="../SiteAssets/jquery.min.js" type="text/javascript"></script>
<script src="../SiteAssets/jquery.SPServices.min.js" type="text/javascript"></script>



<script language="javascript" type="text/javascript">
    $(document).ready(function() {
 
        //  Set the 'Title' input field to the current user's "Title" property
        var userName = $().SPServices.SPGetCurrentUser({
            fieldName: "Title"
        });
        $("input[Title='Title']").val(userName);
 
        //  Set the 'User Name' input field to the current user's "UserName" property
        var userName = $().SPServices.SPGetCurrentUser({
            fieldName: "UserName"
        });
        $("input[Title='User Name']").val(userName);
 
        //  Set the 'Last Name' input field to the current user's "LastName" property
        var lastName = $().SPServices.SPGetCurrentUser({
            fieldName: "LastName"
        });
        $("input[Title='Last Name']").val(lastName);
 
        //  Set the 'First Name' input field to the current user's "FirstName" property
        var firstName = $().SPServices.SPGetCurrentUser({
            fieldName: "FirstName"
        });
        $("input[Title='First Name']").val(firstName);
 
        //  Set the 'Work Phone' input field to the current user's "WorkPhone" property
        var userPhone = $().SPServices.SPGetCurrentUser({
            fieldName: "WorkPhone"
        });
        $("input[Title='Work Phone']").val(userPhone);
 
        //  Set the 'Department' input field to the current user's "Department" property
        var userDepartment = $().SPServices.SPGetCurrentUser({
            fieldName: "Department"
        });
        $("input[Title='Department']").val(userDepartment);
 
    });
</script>

1 comment:

  1. Thanks, Ravi. Is there a need to set the PeoplePicker to empty (
    objPeoplePicker.innerHTML="") if it is a new item ("NewForm.aspx")?

    ReplyDelete