Can't get login name from People field

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
User avatar
geir.mathisen
Posts: 5
Joined: Thu Feb 08, 2018

08 Feb 2018

I struggle to get login name from a single value people field into a variable using js.
Here are a number of alternatives that I've tried, but none of them return anything.
// Alternative 1
var ansatt = fd.field('Ansatt').value().dictionaryEntries[0].AccountName;

// Alternative 2
var ansatt = fd.field('Ansatt').value()[0].DisplayText;

// Alternative 3
fd.field('Ansatt').control('ready', function() {
alert(fd.field('Ansatt').value()[0].DisplayText);
});

// Alternative 4
fd.field('Ansatt').control('ready', function() {
var selectedUsers = fd.field('Ansatt').value();
for (var i = 0; i < selectedUsers.length; i++) {
alert('Login: ' + selectedUsers.Key);
}
});

This one works, but returns the user display name
var ansatt = fd.field('Ansatt').value();

I need to get the login info in order to decide if this is current user or not.
(var currentuser = user.get_loginName();)

Any help on how to build the code to pull login info from a people field is appreciated.
Thanks

NB! This is SharePoint Online.

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

09 Feb 2018

Dear Geir,
Try code like this to get an email of the selected user:

Code: Select all

fd.field('Ansatt').value()[0].EntityData.Email
You can also use this code to get the current user's email:

Code: Select all

_spPageContextInfo.userLoginName;
Cheers

User avatar
geir.mathisen
Posts: 5
Joined: Thu Feb 08, 2018

12 Feb 2018

Thanks for responding.
Sorry to say I cannot get any information from your suggested code either.
Here is the complete JS script in my form:
function setFieldProperty() {
var ansatt = fd.field('Ansatt').value();
alert("-->" + ansatt);
var ansatt2 = fd.field('Ansatt').value()[0].EntityData.Email;
alert("-->" + ansatt2);
}
// Calling setFieldProperty on form loading
setFieldProperty();

The first alert displays user display name from the Ansatt-field, but the second does not display anything.
It seems that the script stops/fails when trying to assign a value from the Ansatt-field.
Not even the static text in the alert is displayed.

Am I building the script wrong?

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

13 Feb 2018

Dear Geir,
First of all, what does the browser's console say? Are there any errors? If there are, what are they, can you show?

Also, you can always experiment by typing commands in the console directly, so you don't need to save the form and wait for it to load, just open the form, open the console and start typing different commands to see what is returned. I didn't see an assignment in your code, so I assume fd.field('Ansatt').value()[0].EntityData.Email didn't work.

Try to run these commands from console one by one:

Code: Select all

fd.field('Ansatt').value();
fd.field('Ansatt').value()[0];
fd.field('Ansatt').value()[0].EntityData;
fd.field('Ansatt').value()[0].EntityData.Email;
Finally, it's also important what template you have selected in Forms Designer for People picker? Is it default or SP2010?
PersonFieldTemplateSP2010.png
PersonFieldTemplateSP2010.png (6.1 KiB) Viewed 1950 times
Default is preferable in this case.
Cheers

User avatar
geir.mathisen
Posts: 5
Joined: Thu Feb 08, 2018

13 Feb 2018

Thanks for the tips.
First: Template = Default
There are errors in the console:
Uncaught ReferenceError: setFieldProperty is not defined
at HTMLDocument.<anonymous> (fd_Ansatt_EditForm.aspx?List=24d8e7a6-14f4-4495-b76c-ec9a133924b7&ID=1&Source=https%3A%2F%2F<customer>.sharepoint.com%2Fsites%2Fsamhandle%2FLists%2FAnsatte%2FAllItems.aspx&ContentTypeId=0x010031350CAF280B4446AABBBA2CAA584D49009FED198FD688B54DAD7BC83F64F751C1:642)
at j (plumsail.fd.jquery.js:2)
at Object.fireWith [as resolveWith] (plumsail.fd.jquery.js:2)
at Function.ready (plumsail.fd.jquery.js:2)
at HTMLDocument.J (plumsail.fd.jquery.js:2)
init.js:1 Uncaught DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules
at q (https://static.sharepointonline.com/bld ... s:1:171544)
at https://static.sharepointonline.com/bld ... s:1:172421
at arguments.(anonymous function) (https://static.sharepointonline.com/bld ... js:1:99714)
fd_Ansatt_EditForm.aspx?List=24d8e7a6-14f4-4495-b76c-ec9a133924b7&ID=1&Source=https%3A%2F%2F<customer>..sharepoint.com%2Fsites%2Fsamhandle%2FLists%2FAnsatte%2FAllItems.aspx&ContentTypeId=0x010031350CAF280B4446AABBBA2CAA584D49009FED198FD688B54DAD7BC83F64F751C1:623 Uncaught TypeError: Cannot read property 'Email' of undefined
at Array.<anonymous> (fd_Ansatt_EditForm.aspx?List=24d8e7a6-14f4-4495-b76c-ec9a133924b7&ID=1&Source=https%3A%2F%2F<customer>..sharepoint.com%2Fsites%2Fsamhandle%2FLists%2FAnsatte%2FAllItems.aspx&ContentTypeId=0x010031350CAF280B4446AABBBA2CAA584D49009FED198FD688B54DAD7BC83F64F751C1:623)
at ScriptResource.axd?d=8bissZWfcsnz3MxascnLOTIQgcwNbwN91MwxEJlEjPFhM7H5ybpz8ZjFafJ3kgoSG_g1_Gof522I0ATQdR2tpeZ2K7Xk__TsG42UpO02N_U115h9BbFb_beRmQAG8rjl0urY_r9b3Kulnc3bEUTc0FdFcp9n77aPKEANhaubt4cry4oeUjB_iDEb5f6yAvH60&t=ffffffff85e60464:5
at SP.ClientRequest.$3I_0 (sp.runtime.js:2)
at Array.<anonymous> (ScriptResource.axd?d=8bissZWfcsnz3MxascnLOTIQgcwNbwN91MwxEJlEjPFhM7H5ybpz8ZjFafJ3kgoSG_g1_Gof522I0ATQdR2tpeZ2K7Xk__TsG42UpO02N_U115h9BbFb_beRmQAG8rjl0urY_r9b3Kulnc3bEUTc0FdFcp9n77aPKEANhaubt4cry4oeUjB_iDEb5f6yAvH60&t=ffffffff85e60464:5)
at ScriptResource.axd?d=8bissZWfcsnz3MxascnLOTIQgcwNbwN91MwxEJlEjPFhM7H5ybpz8ZjFafJ3kgoSG_g1_Gof522I0ATQdR2tpeZ2K7Xk__TsG42UpO02N_U115h9BbFb_beRmQAG8rjl0urY_r9b3Kulnc3bEUTc0FdFcp9n77aPKEANhaubt4cry4oeUjB_iDEb5f6yAvH60&t=ffffffff85e60464:5
at Sys.Net.WebRequest.completed (ScriptResource.axd?d=8bissZWfcsnz3MxascnLOTIQgcwNbwN91MwxEJlEjPFhM7H5ybpz8ZjFafJ3kgoSG_g1_Gof522I0ATQdR2tpeZ2K7Xk__TsG42UpO02N_U115h9BbFb_beRmQAG8rjl0urY_r9b3Kulnc3bEUTc0FdFcp9n77aPKEANhaubt4cry4oeUjB_iDEb5f6yAvH60&t=ffffffff85e60464:5)
at XMLHttpRequest._onReadyStateChange (ScriptResource.axd?d=8bissZWfcsnz3MxascnLOTIQgcwNbwN91MwxEJlEjPFhM7H5ybpz8ZjFafJ3kgoSG_g1_Gof522I0ATQdR2tpeZ2K7Xk__TsG42UpO02N_U115h9BbFb_beRmQAG8rjl0urY_r9b3Kulnc3bEUTc0FdFcp9n77aPKEANhaubt4cry4oeUjB_iDEb5f6yAvH60&t=ffffffff85e60464:5)
2theming.js:1 Cannot find theme color: Light1
outlook.office365.com/owa/service.svc/s/GetPersonaPhoto?email=<active user>.@<customer>.onmicrosoft.com&UA=0&size=HR64x64&sc=1518461172173 Failed to load resource: the server responded with a status of 401 (Unauthorized)
portal.office.com/api/myapps/GetAllApps Failed to load resource: the server responded with a status of 500 ()

Tried to enter the suggested commands directly in the console. That gave this result:
fd.field('Ansatt').value();
"<employee name>."
fd.field('Ansatt').value()[0];
"F"
fd.field('Ansatt').value()[0].EntityData;
undefined
fd.field('Ansatt').value()[0].EntityData.Email;
VM4638:1 Uncaught TypeError: Cannot read property 'Email' of undefined at <anonymous>:1:41

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

14 Feb 2018

Dear Geir,
What form are you working with? Is it the Display Form? Or maybe the field Ansatt is set to readonly in editor?
Cheers

User avatar
geir.mathisen
Posts: 5
Joined: Thu Feb 08, 2018

14 Feb 2018

I need to get value from Ansatt-field in both Display and Edit form.
The script in previous post was from the Display form.

This is the current script from the Edit form:
SP.SOD.executeOrDelayUntilScriptLoaded((function() {
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
ctx.load(web);
var user = web.get_currentUser();
user.retrieve();

ctx.executeQueryAsync(function() {
var currentuser = user.get_loginName();
var currentuser = currentuser.slice(18);
var ansatt = fd.field('Ansatt').value()[0].EntityData.Email;
if(currentuser == ansatt){
alert('equal');
}else{
alert('not equal');
}
});
}), "SP.js");

Console displays this error message:
fd_Ansatt_EditForm.aspx?ID=1&source=/sites/samhandle/Lists/Ansatte/fd_Ansatt_DisplayForm.aspx?ID=1:616 Uncaught TypeError: Cannot read property '0' of null
at Array.<anonymous> (fd_Ansatt_EditForm.aspx?ID=1&source=/sites/samhandle/Lists/Ansatte/fd_Ansatt_DisplayForm.aspx?ID=1:616)
at ScriptResource.axd?d=8bissZWfcsnz3MxascnLOTIQgcwNbwN91MwxEJlEjPFhM7H5ybpz8ZjFafJ3kgoSG_g1_Gof522I0ATQdR2tpeZ2K7Xk__TsG42UpO02N_U115h9BbFb_beRmQAG8rjl0urY_r9b3Kulnc3bEUTc0FdFcp9n77aPKEANhaubt4cry4oeUjB_iDEb5f6yAvH60&t=ffffffff85e60464:5
at SP.ClientRequest.$3I_0 (sp.runtime.js:2)
at Array.<anonymous> (ScriptResource.axd?d=8bissZWfcsnz3MxascnLOTIQgcwNbwN91MwxEJlEjPFhM7H5ybpz8ZjFafJ3kgoSG_g1_Gof522I0ATQdR2tpeZ2K7Xk__TsG42UpO02N_U115h9BbFb_beRmQAG8rjl0urY_r9b3Kulnc3bEUTc0FdFcp9n77aPKEANhaubt4cry4oeUjB_iDEb5f6yAvH60&t=ffffffff85e60464:5)
at ScriptResource.axd?d=8bissZWfcsnz3MxascnLOTIQgcwNbwN91MwxEJlEjPFhM7H5ybpz8ZjFafJ3kgoSG_g1_Gof522I0ATQdR2tpeZ2K7Xk__TsG42UpO02N_U115h9BbFb_beRmQAG8rjl0urY_r9b3Kulnc3bEUTc0FdFcp9n77aPKEANhaubt4cry4oeUjB_iDEb5f6yAvH60&t=ffffffff85e60464:5
at Sys.Net.WebRequest.completed (ScriptResource.axd?d=8bissZWfcsnz3MxascnLOTIQgcwNbwN91MwxEJlEjPFhM7H5ybpz8ZjFafJ3kgoSG_g1_Gof522I0ATQdR2tpeZ2K7Xk__TsG42UpO02N_U115h9BbFb_beRmQAG8rjl0urY_r9b3Kulnc3bEUTc0FdFcp9n77aPKEANhaubt4cry4oeUjB_iDEb5f6yAvH60&t=ffffffff85e60464:5)
at XMLHttpRequest._onReadyStateChange (ScriptResource.axd?d=8bissZWfcsnz3MxascnLOTIQgcwNbwN91MwxEJlEjPFhM7H5ybpz8ZjFafJ3kgoSG_g1_Gof522I0ATQdR2tpeZ2K7Xk__TsG42UpO02N_U115h9BbFb_beRmQAG8rjl0urY_r9b3Kulnc3bEUTc0FdFcp9n77aPKEANhaubt4cry4oeUjB_iDEb5f6yAvH60&t=ffffffff85e60464:5)

BUT:
When entering this cmd directly in the console it now returns the correct value: fd.field('Ansatt').value()[0].EntityData.Email;

Field "Ansatt" is set to ReadOnly=False
Is this a requirement for getting field value?

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

14 Feb 2018

Dear Geir,
First of all, I would really recommend to comment out the request code and all the code that is not used to retrieve an email, it just adds complication and right now we just need to get email consistently.

The issue with the code in Edit Form currently is the fact that the field hasn't finished loading when you try to access it. Try to add this code to JavaScript editor on the form instead (Edit Form):

Code: Select all

var interval = setInterval(function(){ getEmail(); }, 1000);

function getEmail() {
    var email = fd.field('UserWithImageAndDetails').value()[0].EntityData.Email;
	if(email){
		clearInterval(interval);
		alert(email);
		//if it works, remove alert and place the rest of your code here:
	}
}
I don't think there is an easy way to get an email from People picker field on Display Form, though. It's just a text control, it doesn't store additional information.
Cheers

User avatar
geir.mathisen
Posts: 5
Joined: Thu Feb 08, 2018

15 Feb 2018

This worked fine.
Thanks for excellent help.

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 8 guests