Problem saving logedin user

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
gaj
Posts: 40
Joined: Wed Feb 01, 2017

01 Sep 2017

Hallo together,
Sometimes (~1 of 10 times) the following Code don´t save the actual logedin user to the field: ''VI_Zust_x00e4_ndiger_Versand":

Code JavaScript Edit: (Editform):
window.loginUser = 'unknown';
SP.SOD.executeOrDelayUntilScriptLoaded(getCurrentUser, 'SP.js');
function getCurrentUser() {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
currentUser = web.get_currentUser();
context.load(currentUser);
context.executeQueryAsync(onSuccess, onFail);
};
function onSuccess(sender, args) {
window.loginUser = currentUser.get_title(); // extract the login name from the account name
};
function onFail(sender, args) {
alert('Error' + args.get_message());
};

Code OnClick (of custom Button):
fd.field('VI_Zust_x00e4_ndiger_Versand').value(window.loginUser);
fd.save().click();

Thank´s for any help

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

01 Sep 2017

Hello, gaj!
The problem here lies in the async request for the login. Since it is async, next line is executed regarless of the fact if it is completed or not.

There can be multiple solutions for this issue. Maybe, you could separate saving from retrieving login into two buttons. Or you might simply move fd.save().click(); inside the request, so it's only triggered after the value is retrieved.

But also, tell me - are you using SharePoint Online? If you are using SP Online, you don't need to execute this request as the login of the current user is always available with this line:

Code: Select all

_spPageContextInfo.userLoginName;
You can also get user display name that way:

Code: Select all

_spPageContextInfo.userDisplayName;
Cheers

gaj
Posts: 40
Joined: Wed Feb 01, 2017

04 Sep 2017

Dear Nikita,

Yes, we are using SPO, but _spPageContextInfo don´t work. You know why?

Now I´m using this Code (on custom button -Click):

SP.SOD.executeOrDelayUntilScriptLoaded(getCurrentUser, 'SP.js');
function getCurrentUser() {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
currentUser = web.get_currentUser();
context.load(currentUser);
context.executeQueryAsync(onSuccess, onFail);
};
function onSuccess(sender, args) {
fd.field('VI_Zuteilungshistory').value('Zugeteilt');
fd.field('VI_Status').value('Zugeteilt');
fd.field('VI_Zust_x00e4_ndiger_Versand').value(currentUser.get_title());
// Speichern und Schließen
fd.save().click();
};
function onFail(sender, args) {
alert('aktueller Login-User konnte nicht ermittelt werden! ' + args.get_message());
};

thx for your help.
I´ll see next week if it works better :)

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

04 Sep 2017

Not sure why it doesn't work. Can you open browser's console on the Form and enter these commands one by one to see what the console says? Send us a screenshot, if you can.

Code: Select all

_spPageContextInfo

Code: Select all

_spPageContextInfo.userDisplayName
As for your code, it should work in all situations now. The item will only save once the user is successfully retrieved and if something goes wrong, the item won't be saved.
Cheers

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 7 guests