Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Has anyone managed to use SPServices to display the current logged in username in a content webpart?

Is this even possible in 2013 (online)?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You must have your reasons why using SPServices for this mundane task, because the CSOM in SharePoint 2010 and even more in 2013 can help you do that using out-of-the-box routines. Of course it is only working in the current Site Collection.

See example here bellow:

ExecuteOrDelayUntilScriptLoaded(init,"sp.js");
var currentUser;

function init(){
 this.clientContext = new SP.ClientContext.get_current();
 this.oWeb = clientContext.get_web();
 currentUser = this.oWeb.get_currentUser();
 this.clientContext.load(currentUser);
 this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
}
function onQuerySucceeded() {
 document.getElementById("currentUser").innerHTML = currentUser.get_loginName(); 
}
function onQueryFailed(sender, args) {
 alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
</script>
<div>Current Logged-In User:<label id="currentUser"></label></div>
share|improve this answer
    
Thanks for the reply, happy to do it anyway that works...but would prefer to achieve this via the SP ui rather than have use visual studio. Thanks for the input. –  Stephen Feb 27 '13 at 21:47
    
actually that code works perfectly directly in the CEWP, reason for which i've proposed it. Just wrap it in a script tab, put it in a .js file, via Notepad, upload it to a Document Library, add in the page a CEWP and reference the JS. –  C. Marius - MVP Feb 27 '13 at 21:53
    
Hi There, I have this working on my site but its returning my full details i.e. i:0#.f|m​embership|developer@cndev.onmicrosoft.com​​​ where i would prefer to just have the Firstname or Firstname surname. Thanks again. –  Stephen Feb 28 '13 at 10:47
    
Stephen, it seems you use Claims - that is why you get the funny Account name. Available should be get_title(), but many other properties are only available via the User Profile (which of course requires at least Standard edition). See here for a exact example sharemuch.com/2012/05/11/… (you need to ask for FirstName and LastName) –  C. Marius - MVP Feb 28 '13 at 14:20
    
Thanks for that...not sure if I have standard edition or not...I have seen this link blogpost vrdmn.blogspot.in/2013/02/… which looks promosing.. –  Stephen Feb 28 '13 at 14:28
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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