Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

how to use user profile with client object model.? I need to fetch some properties in object model. code example will be helpful.

share|improve this question
    
client object model - need to be specific in usage: ECMA/JS or .NET (or REST) ? –  Supriyo SB Chatterjee Jun 4 at 14:20

3 Answers 3

up vote 1 down vote accepted

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

share|improve this answer
    
He specifically asked for CSOM. –  Matthew McDermott Jun 4 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. –  Supriyo SB Chatterjee Jun 4 at 14:11
    
Ahh, good point. Had not considered .Net. –  Matthew McDermott Jun 4 at 15:44
    
thank you all..:) –  Rajeev K Jun 10 at 6:41

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

share|improve this answer
    
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 Jun 4 at 3:03
    
what kind of properties you want to fetch? Also which version of SharePoint are you using? –  Varun Verma Jun 5 at 7:06

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();
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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