Monday, 18 July 2016

Load All UserProfile properties CSOM


Load All the UserProfile properties of a Logged in User 



Below code will load all user profile properties of a current logged in user in CSOM


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;
using System.Net;


namespace UserProfilePropertiesCSOM
{
    class Program
    {
        static void Main(string[] args)
        {

            ClientContext ctx = new ClientContext("http://Sharepointdevsite:3636/");
            var cuser = ctx.Web.CurrentUser;
            ctx.Load(cuser); //Load the Current Logged User
            ctx.ExecuteQuery();       
            PeopleManager peopleManager = new PeopleManager(ctx);        
            PersonProperties userProperites = peopleManager.GetPropertiesFor(cuser.LoginName);
            ctx.Load(userProperites, p => p.AccountName, p =>
            p.UserProfileProperties);
            ctx.ExecuteQuery();
            foreach (var property in userProperites.UserProfileProperties)
            {
                Console.WriteLine(string.Format("{0}: {1}",
                property.Key.ToString(), property.Value.ToString()));
            }
            Console.ReadKey(false);

        }
    }
}

Ouput:





No comments:

Post a Comment