0

I am facing a issue.I am adding a web-part in which I am showing current user logged in script editor through embedded code.I want to show current user when sharepoint open.I am getting on click but i want to add when load the page.How can i do it.

Code for clicking a button and get the current user in sharepoint in alert message box

<input type='button' id='id1' value='Get User Name' onclick="GetLoggedInUserName();"/>

<script type="text/javascript">
function GetLoggedInUserName()
{
var context = new SP.ClientContext.get_current();
this.website = context.get_web();
this.currentUser = website.get_currentUser();
context.load(currentUser);
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args)
{
 alert(currentUser.get_loginName());
}

function onQueryFailed(sender, args)
{
alert('request failed ' + args.get_message() + '\n'+ args.get_stackTrace());
}
</script>

How can we do it on load.

Thanx in Advance guys

0

You need to use

_spBodyOnLoadFunctionNames.push("GetLoggedInUserName");

because jQuerys ready function isn't reliable in SharePoint

Check the more information here

  • Did you get it working? – Hardik Jul 4 '16 at 5:42

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.