Question

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());
}
Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top