Read User Profile Properties From SharePoint Online Using CSOM

We may required to retrieve SharePoint Online user profile properties, such as display name, email, title, manager and other business and personal information. In this article, I am going write C# code to get user profile properties programmatically using CSOM. The SharePoint Online SDK assembly Microsoft.SharePoint.Client.UserProfiles.dll includes the class PeopleManager and the PeopleManager class includes the following methods.

  • GetMyProperties – This method returns current user properties.
  • GetPropertiesFor – This method returns all profile properties for a specific person. 
  • GetUserProfilePropertiesFor -This method returns only required properties that you specify

Before proceed, you need to add the assembly reference Microsoft.SharePoint.Client.UserProfiles.dll and include following namespace

using Microsoft.SharePoint.Client; 
using Microsoft.SharePoint.Client.UserProfiles;

Read Current User Profile Properties

First initialize the PeopleManager object and use GetMyProperties() method to retrieve current user profile properties.

public static void GetCurrentUserProfileProperties() 
{ 
    string siteUrl = "https://spotenant-admin.sharepoint.com"; 
 
    // Connect to the sharepoint site client context. 
    ClientContext clientContext = new ClientContext(siteUrl); 
    //clientContext.Credentials = credentials
 
    // Get the PeopleManager object and then get the current user's properties. 
    PeopleManager peopleManager = new PeopleManager(clientContext); 
    PersonProperties myProperties = peopleManager.GetMyProperties(); 
             
    // This request load the AccountName and UserProfileProperties 
    clientContext.Load(myProperties, p => p.AccountName, p => p.UserProfileProperties); 
    clientContext.ExecuteQuery(); 
 
    foreach (var property in myProperties.UserProfileProperties) 
    { 
        Console.WriteLine(string.Format("{0}: {1}", 
            property.Key.ToString(), property.Value.ToString())); 
    } 
}

Get Specific User Profile Properties

To get the specified user profile information, we can use GetPropertiesFor() method by passing target user’s account id. The account of the user, should formatted either as a login name, or as a claims identity, e.g. i:0#.f|membership|[email protected];

public static void GetTargetUserProfileProperties() 
{ 
    string siteUrl = "https://spotenant-admin.sharepoint.com"; 
    string targetUser = "i:0#.f|membership|[email protected]"; 
 
    // Connect to the sharepoint site client context. 
    ClientContext clientContext = new ClientContext(siteUrl); 
    // clientContext.Credentials = your credentials
 
    // Get the PeopleManager object and then get the target user's properties. 
    PeopleManager peopleManager = new PeopleManager(clientContext); 
    PersonProperties userProperties = peopleManager.GetPropertiesFor(targetUser); 
 
    // This request load the AccountName and user's all other Profile Properties 
    clientContext.Load(userProperties, p => p.AccountName, p => p.UserProfileProperties); 
    clientContext.ExecuteQuery(); 
 
    foreach (var property in userProperties.UserProfileProperties) 
    { 
        Console.WriteLine(string.Format("{0}: {1}", 
            property.Key.ToString(), property.Value.ToString())); 
    } 
}

Get Only Required Profile Properties

You can use the following C# code to get only specific set of user properties.

public static void GetSpecificProfileProperties() 
{ 
    string siteUrl = "https://spotenant-admin.sharepoint.com"; 
    string targetUser = "i:0#.f|membership|[email protected]"; 
 
    // Connect to the sharepoint site client context. 
    ClientContext clientContext = new ClientContext(siteUrl); 
    // clientContext.Credentials = your credentials
 
    // Get the PeopleManager object. 
    PeopleManager peopleManager = new PeopleManager(clientContext); 
    // Retrieve specific properties by using the GetUserProfilePropertiesFor method.  
    string[] profilePropertyNames = new string[] { "Manager", "Department", "Title" }; 
    UserProfilePropertiesForUser profilePropertiesForUser = new UserProfilePropertiesForUser( 
        clientContext, targetUser, profilePropertyNames); 

    IEnumerable<string> profilePropertyValues = peopleManager.GetUserProfilePropertiesFor(profilePropertiesForUser); 
  
    // Load the request for the set of properties. 
    clientContext.Load(profilePropertiesForUser); 
    clientContext.ExecuteQuery(); 
 
    // Returned collection contains only property values 
    foreach (var value in profilePropertyValues) 
    { 
        Console.WriteLine(value); 
    } 
}

Advertisement

2 thoughts on “Read User Profile Properties From SharePoint Online Using CSOM”

  1. Thank you!
    Can you help me with this: get a specific profile property (path to profile picture for example) for ALL of my Sharepoint's website users in one request? In your example, this would be the targetUser that needs to be filled dynamically. I have > 28000 users… would be great if you can help me out.

    Reply

Leave a Comment