People Picker Field
-
- Posts: 9
- Joined: Wed Jun 03, 2015
Hi!
I need the value of an people picker field as text like 'Andy Weiße'.
If I used this:
fd.field('User').control('ready', function() {
var selectedUsers = fd.field('User').value();
for (var i = 0; i < selectedUsers.length; i++) {
alert('Login: ' + selectedUsers.Key);
}
});
the answer is : domain\weisse ..but I need it in text to compare.
So I need the display name, not the login name
.How can I solve this?
Regards Andy
I need the value of an people picker field as text like 'Andy Weiße'.
If I used this:
fd.field('User').control('ready', function() {
var selectedUsers = fd.field('User').value();
for (var i = 0; i < selectedUsers.length; i++) {
alert('Login: ' + selectedUsers.Key);
}
});
the answer is : domain\weisse ..but I need it in text to compare.
So I need the display name, not the login name
.How can I solve this?
Regards Andy
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
Hi Andy,
Please, try the following code:
Please, make sure that you use the people piker in the Client mode. You can switch the rendering mode in Forms Designer, property name is Render.
Please, try the following code:
Code: Select all
fd.field('User').control('ready', function() {
var selectedUsers = fd.field('User').value();
for (var i = 0; i < selectedUsers.length; i++) {
alert('Login: ' + selectedUsers[i].DisplayText);
}
});
-
- Posts: 9
- Joined: Wed Jun 03, 2015
Hi Dmitry!
Thanks for the answer!
Unfortunately, the same result with Display text. domain\weisse
For Explanation. I try to compare the current user with a user in a field.
var getWebUserData = function() {
context = new SP.ClientContext.get_current();
web = context.get_web();
currentUser = web.get_currentUser();
currentUser.retrieve();
context.load(web);
context.executeQueryAsync(function(o, e) {
user = web.get_currentUser();
currentuser = user.get_title())
}, function(o, e) {
alert('request failed ' + e.get_message() + '\n' + e.get_stackTrace());
});
}
ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
fd.field('Antragsteller').control('ready', function() {
var selectedUsers = fd.field('Antragsteller').value();
for (var i = 0; i < selectedUsers.length; i++) {
alert('Login: ' + selectedUsers.DisplayText);
fielduser = selectedUsers.DisplayText;
}
});
if(currentuser == fielduser)
{
.....
};
Thanks for the answer!
Unfortunately, the same result with Display text. domain\weisse
For Explanation. I try to compare the current user with a user in a field.
var getWebUserData = function() {
context = new SP.ClientContext.get_current();
web = context.get_web();
currentUser = web.get_currentUser();
currentUser.retrieve();
context.load(web);
context.executeQueryAsync(function(o, e) {
user = web.get_currentUser();
currentuser = user.get_title())
}, function(o, e) {
alert('request failed ' + e.get_message() + '\n' + e.get_stackTrace());
});
}
ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
fd.field('Antragsteller').control('ready', function() {
var selectedUsers = fd.field('Antragsteller').value();
for (var i = 0; i < selectedUsers.length; i++) {
alert('Login: ' + selectedUsers.DisplayText);
fielduser = selectedUsers.DisplayText;
}
});
if(currentuser == fielduser)
{
.....
};
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
You'd better compare users by login names. Please, use the following code to get the login name of the current user:
Code: Select all
user.get_loginName()
-
- Posts: 9
- Joined: Wed Jun 03, 2015
Yes that works!
Thanks for your help! Its so easy when you know how it works.
Have a nice day!
Thanks for your help! Its so easy when you know how it works.
Have a nice day!
-
- Posts: 9
- Joined: Wed Jun 03, 2015
Sorry, it is not working properly.
This is my source code:
var context = null;
var web = null;
var currentUser = null;
var user = null;
var currentuser;
var fielduser;
var fieldgenehmiger1;
//Check current user
var getWebUserData = function() {
context = new SP.ClientContext.get_current();
web = context.get_web();
currentUser = web.get_currentUser();
currentUser.retrieve();
context.load(web);
context.executeQueryAsync(function(o, e) {
user = web.get_currentUser();
currentuser = user.get_loginName();
//Check field user
fd.field('Genehmiger1').control('ready', function() {
var selectedUsers = fd.field('Genehmiger1').value();
for (var i = 0; i < selectedUsers.length; i++) {
fieldgenehmiger1 = selectedUsers.Key;
}
});
alert(currentuser + ' compare ' + fieldgenehmiger1); <----this is the message 'i:0#.w|domain\sptest1 compare domain\sptest1'
if(currentuser == fieldgenehmiger1){
alert('equal');
}else{
alert('not equal'); <---- result 'not equal'
}
}, function(o, e) {
alert('request failed ' + e.get_message() + '\n' + e.get_stackTrace());
});
}
ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
//End source code
I had also nested functions, because the result of the functions sometimes was undefined.
(In firefox the result of the 'Check field user' function is almost always undefinded.
I did not think that it is difficult to compare two values.
This is my source code:
var context = null;
var web = null;
var currentUser = null;
var user = null;
var currentuser;
var fielduser;
var fieldgenehmiger1;
//Check current user
var getWebUserData = function() {
context = new SP.ClientContext.get_current();
web = context.get_web();
currentUser = web.get_currentUser();
currentUser.retrieve();
context.load(web);
context.executeQueryAsync(function(o, e) {
user = web.get_currentUser();
currentuser = user.get_loginName();
//Check field user
fd.field('Genehmiger1').control('ready', function() {
var selectedUsers = fd.field('Genehmiger1').value();
for (var i = 0; i < selectedUsers.length; i++) {
fieldgenehmiger1 = selectedUsers.Key;
}
});
alert(currentuser + ' compare ' + fieldgenehmiger1); <----this is the message 'i:0#.w|domain\sptest1 compare domain\sptest1'
if(currentuser == fieldgenehmiger1){
alert('equal');
}else{
alert('not equal'); <---- result 'not equal'
}
}, function(o, e) {
alert('request failed ' + e.get_message() + '\n' + e.get_stackTrace());
});
}
ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
//End source code
I had also nested functions, because the result of the functions sometimes was undefined.
(In firefox the result of the 'Check field user' function is almost always undefinded.
I did not think that it is difficult to compare two values.
For me, the name appears correctly: First name Last name:
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() {
fd.field('AssignedTo').control().value(user.get_loginName());
fd.field('AssignedTo').control()._el().find('a:eq(0)').click();
});
}), "SP.js");
Simply insert in JavaScript editor.
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() {
fd.field('AssignedTo').control().value(user.get_loginName());
fd.field('AssignedTo').control()._el().find('a:eq(0)').click();
});
}), "SP.js");
Simply insert in JavaScript editor.
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
Have you tried to use groups functionality to create different forms depending on field values?
http://spform.com/documentation/groups
There is a function: ContainsCurrentUser([FieldName])
http://spform.com/documentation/groups
There is a function: ContainsCurrentUser([FieldName])
-
- Posts: 9
- Joined: Wed Jun 03, 2015
So it would work wonderfully, yes. So I had made it to show specific users different forms.
But my boss says that everything has to work in a single Edit form. Otherwise it would be difficult to maintain.
And therefore I need to compare field user and current user with javascript.
But my boss says that everything has to work in a single Edit form. Otherwise it would be difficult to maintain.
And therefore I need to compare field user and current user with javascript.
-
- Posts: 9
- Joined: Wed Jun 03, 2015
I think I have it. I have changed the field to server rendering mode. SP.SOD.executeOrDelayUntilScriptLoaded((function() {
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
ctx.load(web);
user = web.get_currentUser();
user.retrieve();
ctx.executeQueryAsync(
function() {
currentuser = user.get_loginName();
fieldgenehmiger1 = fd.field('Genehmiger1')
.value().dictionaryEntries[0].AccountName;
alert(currentuser + ' compare ' + fieldgenehmiger1); <--- now the result is 'i:0#.w|domain\sptest1 compare i:0#.w|domain\sptest1' if(user.get_loginName() == fieldgenehmiger1){
alert('equal');
}else{
alert('not equal'); <--- and the if else function works too!
}
});
}), "SP.js"); ...I will test again exactly on Monday. Oh man, what a crap! Thank you again Dmitry and RMIC. I wish you a nice weekend!
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
ctx.load(web);
user = web.get_currentUser();
user.retrieve();
ctx.executeQueryAsync(
function() {
currentuser = user.get_loginName();
fieldgenehmiger1 = fd.field('Genehmiger1')
.value().dictionaryEntries[0].AccountName;
alert(currentuser + ' compare ' + fieldgenehmiger1); <--- now the result is 'i:0#.w|domain\sptest1 compare i:0#.w|domain\sptest1' if(user.get_loginName() == fieldgenehmiger1){
alert('equal');
}else{
alert('not equal'); <--- and the if else function works too!
}
});
}), "SP.js"); ...I will test again exactly on Monday. Oh man, what a crap! Thank you again Dmitry and RMIC. I wish you a nice weekend!
-
- Information
-
Who is online
Users browsing this forum: No registered users and 27 guests