How to Get a Specific User Profile Property For a User Using CSOM in SharePoint 2013

In this article we will see how to programmatically get a specific user's profile property for a user using CSOM in SharePoint 2013.

Introduction

In SharePoint 2010 using the client-side object model we are unable to retrieve the user profile properties whereas in SharePoint 2013 they have added a new assembly for the user profile service. Using "Microsoft.SharePoint.Client.UserProfiles.dll" located in hive 15 of the ISAPI folder we are able to retrieve the user profile properties using CSOM.
 
(Client-Side Object Model). In this article we will see how to programmatically get a specific user's profile property for a user using CSOM in SharePoint 2013.

Prerequisites

The following are the prerequisites:

  1. SharePoint 2013.
  2. Visual Studio 2012.
  3. User Profile Service application provisioned.
  4. User profiles should be created.

I have created a user profile whose account name is "Pai". In this article we will see how to retrieve a specific user's profile property (Account Name) for the user "Pai" using the Client-Side Object Model.

Create a console application in Visual Studio 2012 using the following:

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

    Share1.jpg
     
  4. Enter the Name and click on Ok.
  5. In the Solution Explorer, right-click on the References folder and then click on Add Reference.
  6. 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
    c. Microsoft.SharePoint.Client.UserProfiles.dll
  7. Open Program.cs file and replace the code with the following:

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://c4968397007:3695/";

 

            //// String Variable to store the account name of the user for whom to get the user profile property

            string accountName = "Pai";

 

            //// String Variable to store the user profile property

            string userProfileProperty = "AccountName";

 

            //// 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);

 

            //// GetUserProfilePropertyFor method is used to get a specific user profile property for a user

            ClientResult<string> profileProperty = peopleManager.GetUserProfilePropertyFor(accountName, userProfileProperty);           

            clientContext.ExecuteQuery();

 

            //// Display the user profile property value for the user

            Console.WriteLine("Account Name: " + profileProperty.Value);         

            Console.ReadLine();

        }

    }

}

Note

In the code snippet the ExecuteQuery() method is used to send the request to the server until the ExecuteQuery() method is called; only the requests are registered by the application. The Load() method does not actually load anything; when the ExecuteQuery() method is called, it only specifies that these are the property values to be loaded for the object. In the Load() method lamda expressions are used to specify which property should be loaded instead of loading all the properties.

Run the console application

Hit F5. The output will be displayed as in the following:

Share2.jpg

Summary

Thus in this article we have seen how to programmatically get a specific user's profile property for a user using CSOM in SharePoint 2013.

Up Next
    Ebook Download
    View all
    Learn
    View all