Current logged in user details

Dears,
i need to get the currently logged in user name and manager and assign them to person fields in my sharepoint online list.
i have tried the following expression :

fd.field('EMP').value = _spPageContextInfo.userLoginName;
EMP is a person field addede to the new form.

but it is giving me the name of another user not the logged in one.
is there any suggestion from anybody.

Dear @MehmetMuhanna,
It should give the current user, unless there is something wrong with the setup. There are other ways to get the info about the current user, more in this article - Populate SharePoint form fields with profile information — SharePoint forms

i have check with microsoft and they said that this is pure Java issue, and they can not solve it

Dear @MehmetMuhanna,
As I've said, this depends on your setup, and there are other ways to retrieve user's email, for example, try this:

function updateCurrentUserInfo() {
    pnp.sp.profiles.myProperties.get().then(function(result) {

        var props = result.UserProfileProperties;
        console.log(props);
        for (var i = 0; i < props.length; i++) {
            switch (props[i].Key) {
                case 'WorkEmail':
                    fd.field('Title').value = props[i].Value;
                    break;
            }
        }
    });
}

fd.spRendered(function() {
    //executes updateCurrentUserInfo on form load
    updateCurrentUserInfo();
});
1 Like

thank you very much, it worked fine.

@Nikita_Kurguzov
Good day! in sharepoint 2019, i have the same problem...
for example there are 2 users with some identic account.
when i use _spPageContextInfo.userDisplayName and enter the second user - olesea.luchian it returns the first user and populate department ,email, etc of first user..
i tried userEmail the same.
userId... - can't use it , because there some internal office Code. which for example if it return 58, it can be another person too...
how can obtain unique current user ?
image

Dear @ixxxl,
Have you tried the code above with user profile properties and WorkEmail property?

@Nikita_Kurguzov
Yes ,tried the code above the same result as _spPageContextInfo.userEmail

Dear @ixxxl,
Using email address should select the correct user. Are you sure that everything is setup correctly in user profiles? Can you check the emails of the users?

Also, check the value when you select it manually:

fd.field('User').$on('change',function(value){
    console.log(value);
});

Expand the value in the browser's console and check what the value is when selected manually.

@Nikita_Kurguzov
it is ok with user email in console , the are different.
the problem is only with populating Person field with current user, and then getting userInfo from this people picker.
instead i think i need to use as you say previously -

pnp.sp.profiles.myProperties.get().then(function(result)

and then populate a text field with current user Display Name, and not Person field. because with person the same trouble, it selects the first one user of two similar names.

Dear @ixxxl,
Yes, it should work with the email as users might even have completely matching names, but the emails are always different.

@Nikita_Kurguzov
good day..
my problem is continued.
when i try to populate person field with _spPageContextInfo.userLoginName or _spPageContextInfo,.userEmail. it returns first user in search of person field and not the correct user.
in screen is example of manual selecting user.
at populating is the same. when Diana V.Botanri created item in person field is returned Diana Botnariuc the first one. in list Created by is corect person - Diana V.Botnari. and workflow works fine. Problem only with populating person field.

Screen from item history:
image

Utilizator is - person field , screen from JS
image

Dear @ixxxl,
If you enter the email manually, is there the same issue? If it's a default SharePoint form not customized with Plumsail Forms, does it work any different?

@Nikita_Kurguzov
i create a new list,without plumsail form, added a person field(single) - put email and it show 2 variants of users. the same issue, only that it sorts another way, it seems from the end to begin

with the plumsail form sorting from a=>b
image

In both cases, when i put manually email i must select correct user with mouse.
in autopopulating.it seems that return first fined.

P.S
i tried with pnp.myproperties to put email people picker - the same wrong result

Dear @ixxxl,
In this case, the issue is not with forms, we use the same end points that regular SharePoint forms use. You might need to adjust user data in Azure AD.

@Nikita_Kurguzov
i have SP2019 and active directory.

Dear @ixxxl,
If the issue is the same without our forms - it's not something we can fix. We use the same endpoints that regular SharePoint forms use.

You need to go into active directory and see what's wrong with users that give you this issue.

@Nikita_Kurguzov
this one , works good for me. but it is in button, and update works only for existing item.
how can i use it for new item form? is it a way to get sharepoint user id ?

pnp.sp.web.lists.getById("81590e02-29ed-4f5c-8927-1481662dd60d").items.getById(fd.itemId).update({
USerId: _spPageContextInfo.userId
}).then(function () {
  fd.close()
});

Dear @ixxxl,
Unfortunately, the field on the form can only be populated with email. User ID might work for populating field, that's not on the form, applying changes directly to the item itself with pnpjs.

Good day @Nikita_Kurguzov ,
may be you know some updates about this issue ?