1

Hello I used this js code in content editor it’s works on my name after save it and share it with my colleague it’s not updating to show his name it’s still shows my name on my friend page

This is the code i used

<div id="displayName"></div>  
<script type="text/javascript">
 var clientContext = new  SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);
 var user = clientContext.get_web().get_currentUser();
 clientContext.load(user);
 clientContext.executeQueryAsync(onUserNameSuccess, onUserNameFail);
function onUserNameSuccess() {
document.getElementById("displayName").innerText = "Welcome, " + user.get_title();
//To display last name first and for cases where full name consists of only two words.
//var userToken = user.get_title().split(' ');
//document.getElementById("displayName").innerText = "Welcome, " + userToken[1] + " " + userToken[0];

}

function onUserNameFail(args) {
document.getElementById("displayName").innerText = 'Error:' + args.get_message();
}
//recommended to use  ExecuteOrDelayUntilScriptLoaded on sp.js
</script>
1
  • Hello, you can use "console.log(user);" line as the first line of " onUserNameSuccess()" function to see the 'user' object details fetched in 'Developer tools' for your browser. Also, try opening the page in 'Incognito/ private' browsing mode in your colleague's laptop with the browser of your choice.
    – UBK
    Commented Apr 20, 2020 at 2:09

2 Answers 2

0

The code is pretty straightforward it should give you a dynamic values. (Though in my opinion I would prefer rest api than csom).

Im assuming the script or page its currently checked in or published, this could be 1 reason why the change is not happening on the other users. Can you double check if the page or script has been published? then lets get other assumptions after.

Let me know if the concept helps! Happy Learning!

0

REST API is more robust and recommended than the javascript which is discussed here.

For the current code troubleshooting, I think you try cleaning the cache or the user variable before it used.

For example :

You can call window.location.reload(true) to reload the current page. It will ignore any cached items and retrieve new copies of the page, css, images, JavaScript, etc from the server. This doesn't clear the whole cache, but has the effect of clearing the cache for the page you are on.

Source:

Clear the cache in JavaScript

Or you can use the below :

localStorage.clear()

Or 

 sessionStorage.clear()

Or

Before using the user variable clear the value if anything is assign to it.

For example :

var user = undefined; // unset        

delete(user); // this removes the variable completely

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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