How to Get the Current Users Properties Using CSOM in SharePoint 2013

Open Visual Studio 2012 (Run as administrator).

Go to File=> New => Project.

Select Console Application in the Visual C# node from the installed templates. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. using Microsoft.SharePoint.Client.UserProfiles;  
  8.   
  9. namespace UserProfileService  
  10. {  
  11.     class Program  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.             //// String Variable to store the siteURL  
  16.             string siteURL = "http://gowtham/";  
  17.   
  18.             //// To get the context of the SharePoint site to access the data  
  19.             ClientContext clientContext = new ClientContext(siteURL);  
  20.   
  21.             //// PeopleManager class provides the methods for operations related to people  
  22.             PeopleManager peopleManager = new PeopleManager(clientContext);  
  23.   
  24.             //// PersonProperties class is used to represent the user properties  
  25.             //// GetMyProperties method is used to get the current user's properties  
  26.             PersonProperties personProperties = peopleManager.GetMyProperties();  
  27.             clientContext.Load(personProperties, p => p.AccountName, p => p.Email, p => p.DisplayName);  
  28.             clientContext.ExecuteQuery();  
  29.             //// Display the user properties - AccountName, Email, DisplayName  
  30.             Console.WriteLine("Account Name: " + personProperties.AccountName);  
  31.             Console.WriteLine("Email: " + personProperties.Email);  
  32.             Console.WriteLine("Display Name: " + personProperties.DisplayName);  
  33.             Console.ReadLine();  
  34.         }  
  35.     }  
  36. }  

Hit F5. The output will be displayed in a console window.

Note: Please add a assembly reference in a visual studio solution explorer.

Add the following assemblies from hive 15 (C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI).

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll