User name when completing New Form

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
Nick Jones
Posts: 4
Joined: Wed Nov 28, 2018

25 Jun 2019

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

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

25 Jun 2019

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 3509 times
Cheers

Leslie
Posts: 32
Joined: Thu Dec 05, 2019

05 Dec 2019

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 ?

User avatar
mnikitina
Posts: 264
Joined: Wed Jun 05, 2019

06 Dec 2019

Hello Leslie,

This code should work in all browsers.

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

Leslie
Posts: 32
Joined: Thu Dec 05, 2019

10 Dec 2019

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 ?

Leslie
Posts: 32
Joined: Thu Dec 05, 2019

10 Dec 2019

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 !

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 15 guests