5

I need to fetch some properties. How can I access User Profile with Client Object Model?

Any code example will be helpful.

1
  • client object model - need to be specific in usage: ECMA/JS or .NET (or REST) ? Commented Jun 4, 2014 at 14:20

4 Answers 4

3

PowerShell would be another way that's fast as well using the following steps:

  1. Get the User Profile from the SP Site (given the login name)
  2. Get all the properties from the UserProfile 'object'
  3. Get/Set individual property of the UserProfile.

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

3
  • He specifically asked for CSOM. Commented Jun 4, 2014 at 11:29
  • I had interpreted as using the .NET Client Object Model in the original question. I have updated above response with using PowerShell and REST API calls. Commented Jun 4, 2014 at 14:11
  • Ahh, good point. Had not considered .Net. Commented Jun 4, 2014 at 15:44
3

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:

  • Microsoft.Office.Server
  • Microsoft.Office.Server.UserProfiles
  • Microsoft.SharePoint
  • 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

2
  • I need to fetch these properties in client object model...using ECMA script, to be specific first of all I want to access current logged in user and then its respective properties.
    – Rajeev K
    Commented Jun 4, 2014 at 3:03
  • what kind of properties you want to fetch? Also which version of SharePoint are you using? Commented Jun 5, 2014 at 7:06
1

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();
}
0

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;

Click Here .You will see more details

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.