zondag 11 januari 2015

Check or Get People Picker value.


New function to check if a people picker field is empty or to get the value of a people picker field.
Functions that may be usefull to use in presave actions when editing or creating new list items. 

The function IsPeoplePickerEmpty must be called with a parameter that contains the title of your field. Or with the value of the text that stands on the left side of your people picker field. 

The function  GetPeoplePickerValue must be called with a parameter that contains the title of your field. Or with the value of the text that stands on the left side of your people picker field. It returns a string containing the key of your people picker field. This is the domain and userid of the inserted value. 

Remark. The GetPeoploePickerValue only works with a single user People Picker field. 

 
function IsPeoplePickerEmpty( titel )
{
  if (( $('nobr:contains("'+ titel +'")').closest("td").next("td").find('input')[1].value  == " ") || ( $('nobr:contains("'+ titel +'")').closest("td").next("td").find('input')[1].value  == "") )
         return true;
  else
         return false;
}
function GetPeoplePickerValue(titel)
{
    var result = "";
        if (!IsPeoplePickerEmpty(titel))
        {
                result = $('nobr:contains("'+ titel +'")').closest("td").next("td").find('input')[1].value;
                result = result.substring(result.indexOf("key=") + 5 );
                result = result.substring(0,result.indexOf("'"));
        }
    return result;
}

Set a PeoplePicker Field without a GUID !


A new function that lot's of people may help if they want to set a value of a People Picker field. 
There are examples to find on the internet how you may do it by using the GUID of the field. This because the People Picker field is made of different html parts. You can't set a value in the field directly by calling his Title property like most of the other SharePoint Fields.
But next function by calling the label of the field (the text you have giving on the screen), and a value (Full name of a person or the userid of a person)


Enjoy the new function !! 

Remark, the function only works for setting one value to the people picker field. 
test
// call the new function Set PeoplePicker.
//
SetPeoplePicker (title, userid);

function SetPeoplePicker(My_Title, userid)
{
        try{
                 var ppID = $($('nobr:contains("' + My_Title + '")').closest("td").next("td").find('div')[0]).attr('id');
                 var ppID_userfield = ppID.substring(0,ppID.indexOf("_upLevelDiv"));
                 var a_ppID = ppID_userfield.split('_');
                 var a_ppID_Length = a_ppID.length;
                 var ppID_userfield2 = a_ppID[0] + '$' +  a_ppID[1] + '$' +  a_ppID[2] ;
                 for ( i = 3; i < a_ppID_Length - 4; i++)
                 {
                       ppID_userfield2 = ppID_userfield2 + '_' + a_ppID[i];
                 }
                 ppID_userfield2 = ppID_userfield2 + '$' +   a_ppID[a_ppID_Length - 4] + '$' +   a_ppID[a_ppID_Length - 3] + '$' +   a_ppID[a_ppID_Length - 2] + '$' +   a_ppID[a_ppID_Length - 1] ;
        
                $('#' + ppID ).text(userid);
                //recognize the people picker driver name
                if(!ValidatePickerControl(ppID_userfield )){
                        ShowValidationError();
                        return false;
                }
                var arg=getUplevel(ppID_userfield );
                var ctx = ppID_userfield ;
                EntityEditorSetWaitCursor(ctx);
                WebForm_DoCallback(ppID_userfield2 ,arg,EntityEditorHandleCheckNameResult,ctx,EntityEditorHandleCheckNameError,true);
        }catch(err){
                $('#' + ppID).text("");
        }
}