we can get directly user information by using Server object but how to get this by using Client object model ?
1 Answer
How to work with user profiles via CSOM in SharePoint 2010
It is not supported to access User Profiles via CSOM in SharePoint 2010, but it is possible to archive via UserProfileService SOAP service
Example (SPServices is used here):
$().SPServices({
operation: "GetUserProfileByName",
AccountName: login, // put login name here
completefunc:function(data,status) {
var profile = {};
$(data.responseText).find("PropertyData").each(function(idx, val) {
var $val = $(val);
var name = $val.find("Name").text();
var value = $val.find("Value").text();
profile[name] = value;
});
// At this point the profile object has all profile properties:
console.log(profile.PreferredName);
console.log(profile.WorkEmail);
console.log(profile['SPS-StatusNotes']);
}
});
The list of operations for UserProfileService service could be found here
How to work with user profiles via CSOM in SharePoint 2013
Example: how to retrieve a specific set of user profile properties for a target user:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.UserProfiles;
namespace UserProfilesCSOM
{
class Program
{
static void Main(string[] args)
{
// Replace the following placeholder values with the target SharePoint site and the
// target user.
const string serverUrl = "http://serverName/";
const string targetUser = "domainName\\userName";
// Connect to the client context.
ClientContext clientContext = new ClientContext(serverUrl);
// Get the PeopleManager object.
PeopleManager peopleManager = new PeopleManager(clientContext);
// Retrieve specific properties by using the GetUserProfilePropertiesFor method.
// The returned collection contains only property values.
string[] profilePropertyNames = new string[] { "PreferredName", "Department", "Title" };
UserProfilePropertiesForUser profilePropertiesForUser = new UserProfilePropertiesForUser(
clientContext, targetUser, profilePropertyNames);
IEnumerable<string> profilePropertyValues = peopleManager.GetUserProfilePropertiesFor(profilePropertiesForUser);
// Load the request and run it on the server.
clientContext.Load(profilePropertiesForUser);
clientContext.ExecuteQuery();
// Iterate through the property values.
foreach (var value in profilePropertyValues)
{
Console.Write(value + "\n");
}
Console.ReadKey(false);
// TO DO: Add error handling and input validation.
}
}
}
References
-
-
2@RajeshJoshi, a tip is to make that clear in your post, I see that Vadmin had to do it for you. Jan 28, 2014 at 11:51
-
Microsoft.SharePoint.Client.UserProfiles is not available in Sp2010 Jan 28, 2014 at 11:55
-