Home › Forums › Classic DFFS › Pull user info from a people picker
- This topic has 25 replies, 7 voices, and was last updated 4 years, 4 months ago by Alexander Bautz.
-
AuthorPosts
-
-
January 30, 2016 at 09:22 #10080
This code example show how to pull user info from a people picker (single or multi select), and return the user info – either from the SharePoint userinfo list, or from the SharePoint userprofile service (not available in SP Foundation).
The code is written for user withe DFFS – in the Custom JS textarea.
// Use this code in NewForm or EditForm $.each(spjs.utility.getFieldValue({"fin":"PeoplePicker1","key":"loginName"}),function(i,login){ var ppData = spjs.utility.userInfo(login); console.log(ppData); // User profile is not available in SP Foundation var userProfile = spjs.utility.userProfile(login); console.log(userProfile); }); // Use this in DispForm $.each(spjs.dffs.beforeProperties["PeoplePicker1_ID"],function(i,userId){ var ppData = spjs.utility.userInfo(userId); console.log(ppData); // User profile is not available in SP Foundation var userProfile = spjs.utility.userProfile(ppData.Name); console.log(userProfile); });
Replace “PeoplePicker1” with your FieldInternalName.
Alexander
-
July 27, 2017 at 12:45 #17488
I’m not a programmer, so I need a little more instruction. I’ve added this to the Custom JS textarea and changed PeoplePicker1 to my field name. But how do I tell it what field to populate? And how do I change it if I want to pull in other properties like the phone number?
- This reply was modified 7 years, 4 months ago by Brett Ross.
-
July 27, 2017 at 23:21 #17508
Hi,
Basically you would add a line below the console.log(userProfile) line like this:setFieldValue("TheNameOfYourField",userProfile.name_of_property);
To know which properties you have to select from, open the dev tools (hit F12) and select “Console”. Expand the logged profile object and look at the available properties.
Please note that the console.log line should NOT be active in production – it’s just to show the profile and the properties.
Let me know how this works out.
Alexander
-
September 5, 2017 at 19:42 #18019
Sorry for the dumb question but I typed in the Custom JS the exact Function for new form as you displayed above and replacing PeoplePicker1 with my Internal Field Name… The console log only displays the DFFS load time tracker. And if I add names it displays Empty string. I don’t know what I’m doing wrong…
Attachments:
-
September 6, 2017 at 16:24 #18032
I forgot to show the code I put into custom JS tab on DFFS… here it is.
Attachments:
-
September 6, 2017 at 18:51 #18046
Hi,
My example for NewForm could have been better as this code will run as the form loads – before you have set anything in the people picker.If you are trying to pull info from the pp to write to another field, you must wrap the code in a named function and set up a trigger on form save to call this function.
Alexander
-
September 6, 2017 at 18:57 #18049
I think I figured it out… in the Misc tab on DFFS section Defer loading of DFFS I had to set the time their… I set it to 300ms don’t know if that’s too much will test by reducing…
-
January 7, 2018 at 17:03 #19218
I’ve had this working since July, and all of a sudden now, it’s no longer working. I’m pulling in the Contract Manager’s work phone number from the user profiles. Here’s my code.
$.each(spjs.utility.getFieldValue({“fin”:”Contract_x0020_Manager”,”key”:”loginName”}),function(i,login){
var ppData = spjs.utility.userInfo(login);
console.log(ppData);var userProfile = spjs.utility.userProfile(login);
console.log(userProfile);
setFieldValue(“CM_x0020_Phone_x0020_Number”,userProfile.WorkPhone);
});I’ve also tried wrapping it in a function, then writing a rule that says, any time the Contract Manager field changes, run the function. But that doesn’t work either.
function PopulatePhone ()
{
$.each(spjs.utility.getFieldValue({“fin”:”Contract_x0020_Manager”,”key”:”loginName”}),function(i,login){
var ppData = spjs.utility.userInfo(login);
console.log(ppData);
// User profile is not available in SP Foundation
var userProfile = spjs.utility.userProfile(login);
console.log(userProfile);
setFieldValue(“CM_x0020_Phone_x0020_Number”,userProfile.name_of_property);
});
} -
January 7, 2018 at 17:17 #19220
Nevermind. I see my mistake.
-
January 7, 2018 at 23:49 #19226
I’m glad you figured it out.
Alexander
-
May 17, 2018 at 20:44 #20923
Andrew – I work with Brett who responded above and she can’t figure out where I am screwing up. This WAS working a few days ago and I don’t know what kinked it up.
I have a people picker that is only entered on the NewForm and I want to pull out LastName & FirstName into their own fields. I cannot get LastName FirstName fields to display in the DispForm. They display fine on the EditForm.
Do I need code on all three forms or just one?
This is the code I am using on the NewForm and EditForm
function PopulateLastFirst () { //Pull in Last Name and First Name from Employee field. $.each(spjs.utility.getFieldValue({"fin":"Employee","key":"loginName"}),function(i,login){ var ppData = spjs.utility.userInfo(login); console.log(ppData); // User profile is not available in SP Foundation var userProfile = spjs.utility.userProfile(login); console.log(userProfile); setFieldValue("Last_x0020_Name",userProfile.LastName); setFieldValue("First_x0020_Name",userProfile.FirstName); }); }
This is the code I have on the DispForm
function PopulateLastFirst () { $.each(spjs.dffs.beforeProperties["Employee"],function(i,userId){ var ppData = spjs.utility.userInfo(userId); console.log(ppData); // User profile is not available in SP Foundation var userProfile = spjs.utility.userProfile(ppData.Name); console.log(userProfile); setFieldValue("Last_x0020_Name",userProfile.LastName); setFieldValue("First_x0020_Name",userProfile.FirstName); }); }
I even tried this on advice from Brett… on the DispForm
function PopulateLastFirst () { $.each(spjs.dffs.beforeProperties["Employee"],function(i,userId){ var ppData = spjs.utility.userInfo(userId); console.log(ppData); // User profile is not available in SP Foundation var userProfile = spjs.utility.userProfile(ppData.Name); console.log(userProfile); }); }
Any help you could give would be much appreciated.
-
May 17, 2018 at 21:02 #20927
I am an idiot… I got it to work
-
May 17, 2018 at 23:03 #20931
I’m glad you figured it out!
Best regards,
Alexander -
October 22, 2018 at 12:57 #22586
Hi Alex
My code is similar(if not the same) as Notrega , but unfortunately he did not post what he did to solve his problem , I cant seem to find the difference between your JS above and his/hers .
I’m trying to populate the users title and SipAddress in the respective fields but when I save on edit and New form i doesnt seem to get the information of the user. I have the rule to trigger the function on save but it still get nothing.
Can you help me?
(I did remove _ID from DispForm but it still didnt work)
Attachments:
-
October 22, 2018 at 18:34 #22592
Hi,
Do you see the output in the console (you must hit F12 and select Console) when running the code?Please note that the console.log lines must not be in the production code – these are for debugging only.
Alexander
-
October 23, 2018 at 14:28 #22598
Hi Alex
my code is showing undefined. Even when i test the spjs.utility.userInfo(login); section which i think is to get the user details in the people picker its returning undefined
I also have triggered the function on save in the rules.
fieldInternalName = SP_x0020_Email_x0020_Valid
Thanks
Attachments:
-
October 23, 2018 at 15:11 #22602
Hi Alex
I found the issue it was my JS syntax. my query is trying to set (userprofile) that hasnt been set hence undefined.
function populateSPNameAndEmail()
{
$.each(spjs.utility.getFieldValue({“fin”:”SP_x0020_Email_x0020_Valid”,”key”:”loginName”}),function(i,login){
var ppData = spjs.utility.userInfo(login);
console.log(ppData);
setFieldValue(“Full_x0020_Name”,ppData.Title);
setFieldValue(“Email_x0020_Address”,ppData.SipAddress);
});
}I had the wrong field aswell in the above code for SipAdress.
Thank you for the jQuery its very useful -
October 23, 2018 at 15:19 #22606
I’m glad you figured it out.
Alexander
-
April 3, 2019 at 17:03 #24649
Hey Alexander,
Can this be done in the display form with a multi-person field (for example: to display the loginName of each person in a single multi-person field)?
Thanks!
David -
April 3, 2019 at 22:15 #24661
Hi,
You can use a snippet like this to get the users from a multichoice (or single choice people picker) and get their login name:var userLoginNames = []; jQuery.each(spjs.dffs.beforeProperties.PeoplePicker1_ID,function(i,id){ userLoginNames.push(spjs.utility.userInfo(id).Name); }); console.log(userLoginNames);
Change “PeoplePicker1” with your people picker internal name.
Alexander
- This reply was modified 5 years, 8 months ago by Alexander Bautz.
-
August 18, 2020 at 13:51 #31232
Alexander,
I hope you are well and staying safe.
I have applied this method to retrieve additional information from eight People Picker fields (all single selection fields). I have triggered this function to On-Save rules in both NewItem and EditItem forms. However, I find that it doesn’t always work, where none of the destination fields are populated – this is in 15 of 100 sampled records.
I cannot find a pattern across the 15 failed records
- Some created via NewItem, some subsequently edited in EditItem
- Different users either create or edit failed records
- My firm uses IE, Chrome, and Firefox – but I can’t tell what my users use, but my tests show all three browsers work
Is there something in my code that you see that could cause this 15% failure rate?
function acquireRoleDetail(){ jQuery.each(spjs.utility.getFieldValue({"fin":"Compass_x0020_Delivery_x0020_Man","key":"loginName"}),function(i,login){ var ppData = spjs.utility.userInfo(login); setFieldValue("DeliveryManagerSIP",ppData.SipAddress); setFieldValue("DeliveryManagerSID",ppData.Name.slice(-7).toUpperCase()); }); jQuery.each(spjs.utility.getFieldValue({"fin":"ExecutiveSponsor","key":"loginName"}),function(i,login){ var ppData = spjs.utility.userInfo(login); setFieldValue("ExecutiveSponsorSIP",ppData.SipAddress); setFieldValue("ExecutiveSponsorSID",ppData.Name.slice(-7).toUpperCase()); }); jQuery.each(spjs.utility.getFieldValue({"fin":"FinalApprover","key":"loginName"}),function(i,login){ var ppData = spjs.utility.userInfo(login); setFieldValue("FinalApproverSIP",ppData.SipAddress); setFieldValue("FinalApproverSID",ppData.Name.slice(-7).toUpperCase()); }); jQuery.each(spjs.utility.getFieldValue({"fin":"FinanceManager","key":"loginName"}),function(i,login){ var ppData = spjs.utility.userInfo(login); setFieldValue("FinanceManagerSIP",ppData.SipAddress); setFieldValue("FinanceManagerSID",ppData.Name.slice(-7).toUpperCase()); }); jQuery.each(spjs.utility.getFieldValue({"fin":"LegalContact","key":"loginName"}),function(i,login){ var ppData = spjs.utility.userInfo(login); setFieldValue("LegalContactSIP",ppData.SipAddress); setFieldValue("LegalContactSID",ppData.Name.slice(-7).toUpperCase()); }); jQuery.each(spjs.utility.getFieldValue({"fin":"SourcingCategoryDirector","key":"loginName"}),function(i,login){ var ppData = spjs.utility.userInfo(login); setFieldValue("SourcingCategoryDirectorSIP",ppData.SipAddress); setFieldValue("SourcingCategoryDirectorSID",ppData.Name.slice(-7).toUpperCase()); }); jQuery.each(spjs.utility.getFieldValue({"fin":"SourcingManager","key":"loginName"}),function(i,login){ var ppData = spjs.utility.userInfo(login); setFieldValue("SourcingCategoryManagerSIP",ppData.SipAddress); setFieldValue("SourcingCategoryManagerSID",ppData.Name.slice(-7).toUpperCase()); }); jQuery.each(spjs.utility.getFieldValue({"fin":"VendorManager","key":"loginName"}),function(i,login){ var ppData = spjs.utility.userInfo(login); setFieldValue("VendorManagerSIP",ppData.SipAddress); setFieldValue("VendorManagerSID",ppData.Name.slice(-7).toUpperCase()); }); }
As always, your support and efforts are greatly appreciated!
R’grds – Ben.
Reference:
Version information
Custom DFFS-folder: DFFS_v4.4.5.4_2020-07-04
DFFS Loader: v2
DFFS frontend: 4.4.5.4 – July 04, 2020
DFFS frontend CSS: 4.61 / 4.61
Autocomplete: 1.6.49 – June 28, 2020
Cascading dropdowns: 3.7.40 – June 1, 2020
jQuery: 1.12.4
Lookup: 1.1.20 – March 10, 2020
Resource management: 2.4.5 – August 29, 2019
SPJS-Utility: 1.348 – June 28, 2020
vLookup: 2.2.156 – June 28, 2020 -
August 18, 2020 at 15:13 #31236
Alexander,
As my solution was to replicate the provided code eight times to address eight People Picker fields, plus the symptom is a failure of the bulk function, I’ve broken the bulk function into eight discreet functions…
I hope to see a possible improvement, and am gathering statistics.
R’grds – Ben.
-
August 18, 2020 at 19:45 #31244
It’s hard to tell what could cause it – as long as they don’t use quick edit / datasheet to edit.
Let me know if you find out anything from your statistics and I’ll do my best to help.
Alexander
-
August 20, 2020 at 18:03 #31289
Alexander,
I’ve gathered more statistics to judge effectiveness of my prior vs. current JS method…
Assuming that a given function will never succeed 100% of the time, grouping eight operations into one function produced a 15% failure rate across 100 invocations (one per record).
However, by breaking the eight operations into eight discrete functions each invoked once per record, the failure rate drops dramatically. The next 50 records (8×50=400 invocations) resulted in only four failures, or .01% – which is totally acceptable in my application.
Thank you for your support, I think I have a solution!
R’grds – Ben.
-
August 20, 2020 at 18:46 #31291
Whoops! I embarrass myself – Err: .01% should be 1%.
-
August 20, 2020 at 21:42 #31294
I’m glad you got it running a bit smoother.
Alexander
-
-
AuthorPosts
- You must be logged in to reply to this topic.