Responsive Advertisement

How to Get the Current User's Properties Using CSOM in SharePoint 2013

How to Get the Current User's Properties Using CSOM in SharePoint 2013:
The following are the prerequisites:

SharePoint 2013.
Visual Studio 2012.
User Profile Service application provisioned.
User profiles should be created.
Create a console application in Visual Studio 2012 using the following:

Open Visual Studio 2012 (Run as administrator).
Go to File=> New => Project.
Select Console Application in the Visual C# node from the installed templates.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.UserProfiles;

namespace UserProfileService
    class Program
        static void Main(string[] args)
            //// String Variable to store the siteURL
            string siteURL = "http://gowtham/";          

            //// To get the context of the SharePoint site to access the data
            ClientContext clientContext = new ClientContext(siteURL);

            //// PeopleManager class provides the methods for operations related to people
            PeopleManager peopleManager = new PeopleManager(clientContext);

            //// PersonProperties class is used to represent the user properties
            //// GetMyProperties method is used to get the current user's properties
            PersonProperties personProperties = peopleManager.GetMyProperties();
            clientContext.Load(personProperties, p => p.AccountName, p => p.Email, p => p.DisplayName);
            //// Display the user  properties - AccountName, Email,  DisplayName
            Console.WriteLine("Account Name: " + personProperties.AccountName);
            Console.WriteLine("Email: " + personProperties.Email);
            Console.WriteLine("Display Name: " + personProperties.DisplayName);

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).
a. Microsoft.SharePoint.Client.dll
b. Microsoft.SharePoint.Client.Runtime.dll

Post a Comment