I need to fetch some properties.
How can I access User Profile
with Client Object Model
?
Any code example will be helpful.
PowerShell would be another way that's fast as well using the following steps:
See these two posts for the PowerShell Code (both are similar) -
A Quick Guide to Getting, Setting and Copying User Profile Properties using PowerShell.
SharePoint: Get Set and Copy User Profile Properties using PowerShell.
A listing of SharePoint PowerShell cmdlets at the Microsoft TechNet site - Use Windows PowerShell cmdlets to configure the User Profile service in SharePoint Server 2013.
[UPDATED: May 4th 2014]
Using PowerShell with REST has its benefits in flexibility and faster development -
Invoke-RestMethod for the Rest of Us
More about the Invoke-RestMethod (REST calls in PowerShell)
PowerShell can then be used with SharePoint 2013: Get UserProfile Properties with REST API
This example uses the User Profiles object model to display the user profile properties that exist on the server. Replace servername with an actual value before running the code example. Also add references to the following in your Microsoft Visual Studio project:
System.Web
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
namespace UserProfilesApp { class Program { static void Main(string[] args) {
using (SPSite site = new SPSite("http://servername"))
{
SPServiceContext context =
SPServiceContext.GetContext(site);
ProfileSubtypeManager psm = ProfileSubtypeManager.Get(context);
ProfileSubtype ps = psm.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User));
//Get the properties
ProfileSubtypePropertyManager pspm = ps.Properties;
foreach (ProfileSubtypeProperty profileSubtypeProperty in pspm.PropertiesWithSection )
{
Console.WriteLine(profileSubtypeProperty.Name);
}
}
}
}
}
Here are other references:
How to: Retrieve a User Profile
How to: Retrieve User Profile Properties
How to: Retrieve What's Common Between Two User Profiles
How to: Get Recent User Profile Changes Using the Change Log
For JavaScript you can do something like:
var personProperties;
// Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');
function getUserProperties() {
// Replace the placeholder value with the target user's credentials.
var targetUser = "domainName\\userName";
// Get the current client context and PeopleManager instance.
var clientContext = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
// Get user properties for the target user.
// To get the PersonProperties object for the current user, use the
// getMyProperties method.
personProperties = peopleManager.getPropertiesFor(targetUser);
// Load the PersonProperties object and send the request.
clientContext.load(personProperties);
clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
}
// This function runs if the executeQueryAsync call succeeds.
function onRequestSuccess() {
// Get a property directly from the PersonProperties object.
var messageText = " \"DisplayName\" property is "
+ personProperties.get_displayName();
// Get a property from the UserProfileProperties property.
messageText += "<br />\"Department\" property is "
+ personProperties.get_userProfileProperties()['Department'];
$get("results").innerHTML = messageText;
}
// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
$get("results").innerHTML = "Error: " + args.get_message();
}
using Client Object Model
Solution Explorer, right-click on the "References" folder and then click on "Add Reference"
We have to add ddl:
> Microsoft.SharePoint.Client.dll
> Microsoft.SharePoint.Client.Runtime.dll
> Microsoft.SharePoint.Client.UserProfiles.dll
Namespace:
using Microsoft.SharePoint.Client; using Microsoft.SharePoint.Client.UserProfiles;
Coding:
string siteUserName="nam";
string _siteurl="http://sharepoint.com";
string _sitepassword ="pass@word1";
string accountName ="raghupc\raghu";
ClientContext clientContext = new ClientContext(_siteurl);
clientContext.AuthenticationMode =ClientAuthenticationMode.Default;
clientContext.Credentials = new System.Net.NetworkCredential(_siteUserName, _sitepassword, _domainName);
Web oweb = clientContext.Web;
clientContext.ExecuteQuery();
PeopleManager peopleMang = new PeopleManager(clientContext);
PersonProperties personProp = peopleMang.GetPropertiesFor(accountName);
clientContext.Load(personProp, p => p.AccountName, p => p.Email, p => p.DisplayName);
clientContext.ExecuteQuery();
Label1.Text= personProp.AccountName;
Label2.Text= personProp.Email;
Label3.Text =personProp.DisplayName;