Monday, May 20, 2024
36
rated 0 times [  41] [ 5]  / answers: 1 / hits: 23034  / 14 Years ago, wed, november 24, 2010, 12:00:00

Hallo,



i would like to do something that i thought would be fairly simple:

get the loginName of the current user using the SharePoint2010 Client OM with ECMAScript.

Here is my code:



        var currentcontext = new SP.ClientContext.get_current();
var currentweb = currentcontext.get_web();
currentcontext.load(currentweb);
var currentuser = currentweb.get_currentUser();
currentcontext.load(currentuser);
var loginName = currentuser.get_loginName();


the last line throws an exception:
The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

But why? I've loaded the 'currentuser', so why is the 'property or field' not initialized?


More From » sharepoint-2010

 Answers
7

You need to retrieve the user first and load the web, not the user.



The following should work:



var currentcontext = new SP.ClientContext.get_current();
var currentweb = currentcontext.get_web();
currentcontext.load(currentweb);
var currentuser = currentweb.get_currentUser();
currentuser.retrieve();
currentcontext.load(currentweb);
var loginName = currentuser.get_loginName();


For an even better example using an asynchronous query check the following tutorial:
SharePoint 2010 ECMAScript - How to know logged in user information


[#94863] Saturday, November 20, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryans

Total Points: 514
Total Questions: 92
Total Answers: 121

Location: Liberia
Member since Fri, Oct 22, 2021
3 Years ago
;