Eton B.
Eton B.

Reputation: 6281

Retrieving User Name in Sharepoint?

How does Sharepoint retrieve the user's actual name as displayed in the top right corner? e.g Welcome John Smith

I need to call this name as a variable or parameter on custom code in the XSL editor but I can't figure out how I can retrieve it, is it a global variable?

Upvotes: 2

Views: 6001

Answers (4)

dah97765
dah97765

Reputation: 687

If you want to use SPServices (which is great, btw):

function getCurrentUsersName(){
var firstName = $().SPServices.SPGetCurrentUser({
    fieldName: "FirstName",
    debug: false
});
return firstName;
}

function getCurrentUsersLastName(){
var lastName = $().SPServices.SPGetCurrentUser({
    fieldName: "LastName",
    debug: false
});
return lastName;
}

You can look up a host of other similar fieldnames here:

Upvotes: 1

Kit Menke
Kit Menke

Reputation: 7056

You can get a user's account name using the server variable LOGON_USER. However, this doesn't return a user's Display name.

I was able to get something working which a combination of web parts to display their name:

  1. Add a UserContextFilterWebPart (you may need to enable this web part in the web part gallery).
  2. Add a DataView webpart which queries the GetUserInfo method (part of the UserGroup.asmx web service).
    • a. Click "Connect to a web service..." in the Data Source Library pane under XML Web Services
    • b. Enter the Service description location (the URL to the UserGroup web service). Ex: http://server/sites/SiteCollection/SubSite/_vti_bin/UserGroup.asmx?WSDL
    • c. Click Connect (or reconnect)
    • d. Choose GetUserInfo for the Operation dropdown (the other dropdowns should be ok)
    • e. Modify the userLoginName parameter and check the box to allow the value to be set via web part connection. I also added a default value to test (ex: domain\login).
    • f. Click OK.
    • g. Click on the data source and click Show Data
    • h. Choose the columns you need and then drag them onto the page
  3. Connect them together using web part connections (UserContext provided to the DataView).

Upvotes: 1

Ian
Ian

Reputation: 879

SPContext.Current.Web.CurrentUser.LoginName will give you the value for the login name of the current user, as displayed in the top right of a standard portal.

If you want to use this with XSLT, you need to find a way of assigning this to an XSL parameter value at run-time.

Upvotes: 0

Adam
Adam

Reputation: 818

I believe it's coming in through NTLM Authentication/Active Directory. I ususally get login name DOMAIN/User in the HttpContext.Current.User.Identity.Name field, and then match against Active Directory and bring back the actual name of the user.

Upvotes: 0

Related Questions