0

we can get directly user information by using Server object but how to get this by using Client object model ?

2
  • Are you looking to use Client side/JSOM?
    – Stephen
    Commented Jan 28, 2014 at 11:04
  • Client Side code Commented Jan 28, 2014 at 11:38

1 Answer 1

2

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.
        }
    }
}

Source

References

Work with user profiles in SharePoint 2013

4
  • This code is for SP2013 but i want for SP2010 Commented Jan 28, 2014 at 11:34
  • 2
    @RajeshJoshi, a tip is to make that clear in your post, I see that Vadmin had to do it for you. Commented Jan 28, 2014 at 11:51
  • Microsoft.SharePoint.Client.UserProfiles is not available in Sp2010 Commented Jan 28, 2014 at 11:55
  • The answer has been updated Commented Jan 28, 2014 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.