User name when completing New Form
-
- Posts: 4
- Joined: Wed Nov 28, 2018
Hi
I'm trying to get the user's name autopopulated onto a New form when they are filling it in.
I've tried this, running when the form is opened:
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('Report_x0020_Document_x0020_Cont').control().value(value(user.get_loginName()));
fd.field('Report_x0020_Document_x0020_Cont').control()._el().find('a:eq(0)').click();
alert(value(user.get_loginName()));
});
}), "SP.js");
But it errors, saying that the field ('Report_x0020_Document_x0020_Cont') doesn't exist.
Can you let me know what's going wrong? Or let me know how else I can autopopulate the user name in a New form?
Thanks
I'm trying to get the user's name autopopulated onto a New form when they are filling it in.
I've tried this, running when the form is opened:
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('Report_x0020_Document_x0020_Cont').control().value(value(user.get_loginName()));
fd.field('Report_x0020_Document_x0020_Cont').control()._el().find('a:eq(0)').click();
alert(value(user.get_loginName()));
});
}), "SP.js");
But it errors, saying that the field ('Report_x0020_Document_x0020_Cont') doesn't exist.
Can you let me know what's going wrong? Or let me know how else I can autopopulate the user name in a New form?
Thanks
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 2017
Dear Nick,
Please, try to use the code like this:
Also, make sure to use correct Internal Name for the field:
Please, try to use the code like this:
Code: Select all
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('Person').value(user.get_loginName());
});
}), "SP.js");
Cheers
Indeed !
It seems my issue is about an "if" condition :
if (fd.field('Demandeur').value() == '') {
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('Demandeur').value(user.get_loginName());
});
}), "SP.js");
}
If I don't put the "if" condition, it works, on IE, Firefox and Chrome.
I don't know why this "if" doesn't work with Firefox and Chrome...
Can you help me ?
It seems my issue is about an "if" condition :
if (fd.field('Demandeur').value() == '') {
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('Demandeur').value(user.get_loginName());
});
}), "SP.js");
}
If I don't put the "if" condition, it works, on IE, Firefox and Chrome.
I don't know why this "if" doesn't work with Firefox and Chrome...
Can you help me ?
Found it !
If I put my "if" in the SP.SOD.executeOrDelayUntilScriptLoaded((function () { , it works fine !
This is the final code :
SP.SOD.executeOrDelayUntilScriptLoaded((function () {
if (fd.field('Demandeur').value() == '') {
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('Demandeur').value(user.get_loginName());
});
}
}), "SP.js");
Thank you and have a nice day !
If I put my "if" in the SP.SOD.executeOrDelayUntilScriptLoaded((function () { , it works fine !
This is the final code :
SP.SOD.executeOrDelayUntilScriptLoaded((function () {
if (fd.field('Demandeur').value() == '') {
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('Demandeur').value(user.get_loginName());
});
}
}), "SP.js");
Thank you and have a nice day !
-
- Information
-
Who is online
Users browsing this forum: No registered users and 15 guests