How to get current user login name in Modern script editor webpart?
-
07-02-2021 - |
Pergunta
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());
}
Outras dicas
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:
To Get User Display Name:
var currentUserName=_spPageContextInfo.userDisplayName;
To Get User Login Name:
var currentUserLoginName=_spPageContextInfo.userLoginName;
To Get User Id:
var userID=_spPageContextInfo.userId
Reference: Get user logged on sharepoint online
Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange