How to read UserProfile property from Office 365 SharePoint 2013 site using CSOM

Step#1

Create a console application in Visual Studio 2013

Step#2

Import the following namespaces

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;

using System.Net;
using System.Security;
using System.Globalization;

Step#3

Add references to the following .dll

references

Step#3
 static void Main(string[] args)
        
        {
            ReadUserProfile();
            
           
        }

        public static void ReadUserProfile()
        {
            Uri oUri = new Uri("https://yoursite.sharepoint.com");

            Office365ClaimsHelper claimsHelper = new Office365ClaimsHelper(oUri, "youruserid@yoursite.onmicrosoft.com", "password");
            using (ClientContext oClientContext = new ClientContext(oUri))
                        {
                            oClientContext.ExecutingWebRequest += claimsHelper.clientContext_ExecutingWebRequest;
                PeopleManager oPeopleManager = new PeopleManager(oClientContext);
                PersonProperties oPersonProperties = oPeopleManager.GetPropertiesFor("i:0#.f|membership|youruserid@yoursite.onmicrosoft.com");
                
                oClientContext.Load(oPersonProperties, p => p.AccountName, p => p.UserProfileProperties);
                oClientContext.ExecuteQuery();

                Console.WriteLine(oPersonProperties.UserProfileProperties.Count.ToString());
                
                foreach (var oProperty in oPersonProperties.UserProfileProperties)
                {
                    Console.WriteLine(string.Format("{0}: {1}",
                        oProperty.Key.ToString(), oProperty.Value.ToString()));
                    
                }
                Console.ReadLine();

            }

CSOM userprofile

The account parameter to be passed for the method oPeopleManager.GetPropertiesFor is not straight-forward. For office 365 SharePoint accounts, the parameter has to be passed in this format "i:0#.f|membership|youruserid@yoursite.onmicrosoft.com". Replace the youruserid accordingly.

The complete source code for this article is available in this location

 Subscribe to my blog

1 thought on “How to read UserProfile property from Office 365 SharePoint 2013 site using CSOM

Leave a Reply