Get full name by log in name in sharepoint 2010

Is there an easy way to retrieve fullname e.g "John Smith" from its login name "j.smith" in sharepoint? Ive been ages looking for that code.

I have this code:

   string strName = SPUtility.GetAccountName("j.smith");

Where strName should be my fullname and "j.smith" is my login name. I dont think its correct..any advice from sharepoint masters out there?

Answers


If the user whose full name needs to be fetched is already added to the site or it is Ok that the user be added to the site you can use :

  web.EnsureUser(LoginName).Name

Alternatively you can use User profile service and get the full name of the user from his user profile if the user is not added to the site and you do not want to add that user to the site.

 SPServiceContext context = SPServiceContext.GetContext(site);
 UserProfileManager upm = new UserProfileManager(context);
 UserProfile profile = upm.GetUserProfile(@"zsdev\ia"); 
Console.WriteLine(profile.DisplayName);

If you have a current SPWeb you can use web.EnsureUser(loginName).Name.


Need Your Help

Importing an XML document into a Rails database?

ruby-on-rails xml sqlite

I've been reading through tutorial after tutorial, but nothing seems to be working out for me. The goal is to take a XML document with elements and attributes and insert the data in a database. Each

SQL trigger for audit table

sql sql-server-2008 triggers sql-delete

I am writing a trigger to audit updates and deletes in tables. I am using SQL Server 2008