0

I'm getting the error Object doesn't support property or method 'ExecuteOrDelayUntilScriptLoaded'.

Below is my code.

    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) {
    var acountname = currentUser.get_loginName(); // extract the login name from the account name
    alert(acountname);
}

function onFail(sender, args) {
    alert('Failed to get current user' + args.get_message());
}
1

Try to enable _spPageContextInfo option in Modern Script Editor Web Part: enter image description here

Then test again to see if it works:

enter image description here

enter image description here

0

Best and simple way to get current logged in user name is _spPageContextInfo.

_spPageContextInfo:

_spPageContextInfo is a global variable usually available on any SharePoint page. This variable stores basic information about current web and current user.

Below are the Some examples:

  1. To Get User Display Name:

    var currentUserName=_spPageContextInfo.userDisplayName;

  2. To Get User Login Name:

    var currentUserLoginName=_spPageContextInfo.userLoginName;

  3. To Get User Id:

    var userID=_spPageContextInfo.userId

Reference: Get user logged on sharepoint online

  • Ganesh, I have already tried this but it didn't work. – Priyank Bhavsar May 6 at 14:26
  • Where are you trying to use this code? – Ganesh Sanap May 6 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.