Advertisement

Responsive Advertisement

Programmatically read Current User Profile Properties from SharePoint Online Using CSOM


This sample shows simple operations on how to manipulate user profile using Client Side Object Model (CSOM). It is using the latest SharePoint Online CSOM, which is exposing APIs also to update user profile propertieson SharePoint online - Office 365 sites. The update property option is available only on Office 365 sites as of now and it is not available on on-premise versions.
Code for reading the user profile properties is pretty straight forward. You will need to have reference to Microsoft.SharePoint.Client.UserProfiles assembly which is providing the needed objects for accessing user profile capabilities in the SharePoint.
https://cdn.swc.com/media/banner-sharepoint-vs-groups.jpg
 Read Current User Profile Properties
First initialize the PeopleManager object and use GetMyProperties() method to retrieve current user profile properties.

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.UserProfiles;
using System.Security;

namespace UserProfile.Manipulation.CSOM.Console
{
    class Program
    {
        static void Main(string[] args)
        {   
            //Tenant Admin Details
            string tenantAdministrationUrl = "https://yourtenant-admin.sharepoint.com/";
            string tenantAdminLoginName = "admin@yourtenant.onmicrosoft.com";
            string tenantAdminPassword = "Password";

            //AccountName of the user whose property you want to update.
            //If you want to update properties of multiple users, you can fetch the accountnames through search.
            string UserAccountName = "i:0#.f|membership|anotheruser@yourtenant.onmicrosoft.com";

            using (ClientContext clientContext = new ClientContext(tenantAdministrationUrl))
            {
                SecureString passWord = new SecureString();

                foreach (char c in tenantAdminPassword.ToCharArray()) passWord.AppendChar(c);

                clientContext.Credentials = new SharePointOnlineCredentials(tenantAdminLoginName, passWord);

                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())); 
    } 
            }
        }
    }
}

Post a Comment

0 Comments