1

We are have sync the SP with AD using Active Directory Import for SharePoint 2013.

All the users have been picked up into the Manage User Profiles and new user properties have been added to the profiles.

Is it possible to query this profiles and fetch the properties for loggedin user?

How to do this in:-

  1. JavaScript object model? (say on masterpage)

  2. Server Object Model? ( say usercontrol on master page)

I tried foll. JS code:-

<script>

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 = "mydomain\nachiket";

  // 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() {
alert('success');

alert(personProperties.get_displayName());

}

// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {

alert(args.get_message());
}

</script>

But it doesnt execute the alert(personProperties.get_displayName());

1 Answer 1

2

Choice JS or Server OM depends on you. For example if you think that in the future you will need the same code in SP Online, in that case is better write the code in JS. The main object for that is PeopleManager.

This is an example of JS code:

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

NOTES:

  1. Replace the domainName\userName placeholder value before you run the code.
  2. If you need to release the same code as an App you have to set the permission for the PeopleManager object

Server side code is simplest:

using (SPSite site = new SPSite("http://servername"))
{
  var context = ServerContext.GetContext(site);
  var profileManager = new UserProfileManager(context);
  foreach (UserProfile profile in profileManager)
  {
    Console.WriteLine(profile[PropertyConstants.AccountName]);
  }
}

If you want get information of an user that is not your current user you need to have the permission to get those information.

8
  • Hello, I donot want to create new user profile. User profiles are already imported from AD. I just want to fetch the relevant property of the loggedin user. Is this possible?
    – variable
    Commented Mar 22, 2014 at 7:57
  • Sorry my fault. I updated the C# code Commented Mar 22, 2014 at 8:00
  • Do you suggest me to use a user control or a javascirpt code in master page for my requirement? Based on retreived property value I want to change the css
    – variable
    Commented Mar 22, 2014 at 8:03
  • I suggest you to use JS for the future solution but if you don't think to deploy the solution for Sharepoint Online the code behind solution is ok. In this case you should release a web part without UI Commented Mar 22, 2014 at 8:05
  • I tried your js script, have updated my question as the JS code isnt giving me the display name. Can you check,
    – variable
    Commented Mar 22, 2014 at 10:01

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.