1

I have set up a working connection between my sharepoint 2013 online and my Active directory, what I'm trying to achieve is to retrieve a specific field (in this particular case an email address field) of an user in Active Directory, does anyone knows if there is a way to do that only via client side script (JavaScript)? If so, can anyone provide me some readings or some code? Thanks very much.

IMPORTANT: I don't need/want to import them in a list, I just need to read them directly from AD.

1 Answer 1

2

Considering that you have run a AD synchronization with your UPSA and that your AD connection rules include the OU of the user you are querying for, you should be able to retrieve the specific AD fields (in this case email) using JSOM. Check SP.Userprofiles (https://msdn.microsoft.com/en-us/library/office/jj920104.aspx)

var userProfileProperties;

// 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);

// Specify the properties to retrieve and target user for the 
// UserProfilePropertiesForUser object.
var profilePropertyNames = ["Email"];
var userProfilePropertiesForUser = 
    new SP.UserProfiles.UserProfilePropertiesForUser(
        clientContext,
        targetUser,
        profilePropertyNames);

// Get user profile properties for the target user.
// To get the value for only one user profile property, use the
// getUserProfilePropertyFor method.
userProfileProperties = peopleManager.getUserProfilePropertiesFor(
    userProfilePropertiesForUser);

// Load the UserProfilePropertiesForUser object and send the request.
clientContext.load(userProfilePropertiesForUser);
clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
}

// This function runs if the executeQueryAsync call succeeds.
function onRequestSuccess() {
var messageText = "\"Email\" property is " 
    + userProfileProperties[0];
$get("results").innerHTML = messageText;
 }

     // This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
    $get("results").innerHTML = "Error: " + args.get_message();
}
4
  • Ok, thank you very much, I will check if this working in the next days, meanwhile can I take advantage of you by asking if in your opinion there is also a way to retrieve all email address of all users in an AD group? And how can I distinguish users that cames from AD rather then internal Sharepoint Users?
    – FabioEnne
    May 8, 2015 at 14:26
  • I think your requirement is just to read user information from AD and probably display it in Sharepoint, correct? In that case, you can just use Client ADO with javascript to fetch user details, check this article sharepointknowledgebase.blogspot.com/2013/08/… May 8, 2015 at 14:31
  • @RansherSingh, is this applicable to SharePoint Online? O365 can be restrictive.
    – wjervis
    May 8, 2015 at 16:45
  • You are correct, I don't think that would work for online (haven't tested). Maybe a better approach would be using node.js with O365 API to get information from Azure AD github.com/OfficeDev/O365-Web-Directory May 8, 2015 at 16:55

Your Answer

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

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