Retreive User Profile Properties by using .NET CSOM in Office 365 / SharePoint Online
using System.Text;
using System.Threading.Tasks;
using System.Security;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.UserProfiles;
namespace UserProfileCSOM
{
class Program
{
static void Main(string[] args)
{
const string serverUrl = "<<Site Url>>";
ClientContext cltContext = new ClientContext(serverUrl);
var userAccountName = "i:0#.f|membership|test@test.com";
var myUserName = "test@test.com";
var myPassword = "*****";
var secureStringPassword = new SecureString();
myPassword.ToList().ForEach(c => secureStringPassword.AppendChar(c));
var creds = new SharePointOnlineCredentials(myUserName, secureStringPassword); // Requires SecureString() for password
cltContext.Credentials = creds;
PeopleManager pplManager = new PeopleManager(cltContext);
PersonProperties prsnProperties = pplManager.GetPropertiesFor(userAccountName);
cltContext.Load(prsnProperties, p => p.AccountName, p => p.UserProfileProperties);
cltContext.ExecuteQuery();
foreach (var property in prsnProperties.UserProfileProperties)
{
Console.WriteLine(string.Format("{0}: {1}", property.Key.ToString(), property.Value.ToString()));
}
Console.ReadKey(false);
}
}
}
Result:
using System.Threading.Tasks;
using System.Security;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.UserProfiles;
namespace UserProfileCSOM
{
class Program
{
static void Main(string[] args)
{
const string serverUrl = "<<Site Url>>";
ClientContext cltContext = new ClientContext(serverUrl);
var userAccountName = "i:0#.f|membership|test@test.com";
var myUserName = "test@test.com";
var myPassword = "*****";
var secureStringPassword = new SecureString();
myPassword.ToList().ForEach(c => secureStringPassword.AppendChar(c));
var creds = new SharePointOnlineCredentials(myUserName, secureStringPassword); // Requires SecureString() for password
cltContext.Credentials = creds;
PeopleManager pplManager = new PeopleManager(cltContext);
PersonProperties prsnProperties = pplManager.GetPropertiesFor(userAccountName);
cltContext.Load(prsnProperties, p => p.AccountName, p => p.UserProfileProperties);
cltContext.ExecuteQuery();
foreach (var property in prsnProperties.UserProfileProperties)
{
Console.WriteLine(string.Format("{0}: {1}", property.Key.ToString(), property.Value.ToString()));
}
Console.ReadKey(false);
}
}
}
Result:
Comments
Post a Comment