zondag 11 januari 2015

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("");
        }
}


Geen opmerkingen:

Een reactie posten