Page 1 of 1

User name when completing New Form

Posted: 25 Jun 2019
by Nick Jones
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

Re: User name when completing New Form

Posted: 25 Jun 2019
by Nikita Kurguzov
Dear Nick,
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");
Also, make sure to use correct Internal Name for the field:
InternalName.png
InternalName.png (4.54 KiB) Viewed 3508 times

Re: User name when completing New Form

Posted: 05 Dec 2019
by Leslie
It works but ... only with Internet Explorer !
It doesn't seem to work with Mozilla Firefox, or Google Chrome.

There is something to do for fixing that ?

Re: User name when completing New Form

Posted: 06 Dec 2019
by mnikitina
Hello Leslie,

This code should work in all browsers.

Do you get any errors in the console(F12)? Could you please share the screenshot.

Re: User name when completing New Form

Posted: 10 Dec 2019
by Leslie
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 ?

Re: User name when completing New Form

Posted: 10 Dec 2019
by Leslie
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 !