Forum Discussion
DCady
Jan 16, 2022Copper Contributor
Get Current User Details SharePoint Online
Hey There!
I am currently working on a project and I am utilizing the Script editor on my modern SharePoint page. I have code to display the current user that is signed into SharePoint, within an HTML div. For some reason, the code does not execute. I have also tried many other scripts that I have found with no luck. Is there a proper script to use for this instance? Please see the current code below:
<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("init");
var currentUser;
function init(){
this.clientContext = new SPHttp.ClientContext.get_current();
this.oWeb = clientContext.get_web();
currentUser = this.oWeb.get_currentUser();
this.clientContext.load(currentUser);
this.clientContext.executeQueryAsync(Function.createDelegate(this,onQuerySucceeded), Function.createDelegate(this,onQueryFailed));
}
function onQuerySucceeded() {
document.getElementById('userLoginName').innerHTML = currentUser.get_loginName();
document.getElementById('userId').innerHTML = currentUser.get_id();
document.getElementById('userTitle').innerHTML = currentUser.get_title();
document.getElementById('userEmail').innerHTML = currentUser.get_email();
}
function onQueryFailed(sender, args) {
alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
</script>
<div>Current Logged User:
<span id="userLoginName"></span></br>
<span id="userId"></span></br>
<span id="userTitle"></span></br>
<span id="userEmail"></span></br>
</div>
6 Replies
Sort By
DCady Can you tell us which script editor web part are you using (any link to web part sample)?
I am using this Script Editor web part and I can get the user details using _spPageContextInfo variable (no need of JSOM/REST API call). For example, below code will return the title/display name of user:
_spPageContextInfo.userDisplayName
Similarly you can get user ID, Email & login name as mentioned in this post: Get user logged on sharepoint online .
Here is working example:
<p id="pWelcomeMsg"></p> <script type="text/javascript"> document.getElementById("pWelcomeMsg").innerHTML = "Hey " + _spPageContextInfo.userDisplayName + ", welcome to our site"; </script>
Output:
Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.
- GSkinner76Copper Contributor
ganeshsanap Hi I just saw your solution/help for this member. I am trying to do something similar but in the new list item form ootb sp online. The window that appears on the side of the screen when they click new item i would like the CurrentUser details to be prepopulated is there a way of doing this without using powerapps as the com[pany i am doing this for has no powerapps licenses
GSkinner76 Unfortunately, you cannot add custom SPFx web part on the SharePoint default modern experience list forms.
For SharePoint online modern experience lists, you can either customize the list forms using SPFx or Power Apps.
Power Apps is easier and suitable for your requirements.
What Microsoft 365 (Office 365) license you are using in your company? If you are using E1, E3 or E5, you can use Power Apps for free as long as you use standard connectors.
Check:
- Find the right Microsoft 365 enterprise plan for your organization
- Power Platform - List of all Standard tier connectors
- Power Platform - List of all Premium tier connectors - These require additional Power Apps licenses
Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.
- DCadyCopper Contributor
Thanks for reaching out! I am currently utilizing that same script editor web part, that you have posted below. I am not exactly sure why it's not rendering the code. I have used the script editor to render other JS and HTML code but when it comes to obtaining user information, it will not render on my modern SharePoint page. I have attempted to utilize your example below with no luck. Also, I am an admin with full access.
DCady You have to enable the access to classic _spPageContextInfo from web part settings like:
Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.