0

I have a new requirement from client to insert AD details from the users into a Web Part within a site. I have read that the GetUserProfileByName web service is used for this purpose but when I made the request using jquery - ajax I got 401 Unauthorized.

Code:

function call2WS() {
    var soapEnv = "<?xml version='1.0' encoding='utf-8'?> \
        <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
        <soap:Body> \
            <GetUserProfileByName xmlns='http://microsoft.com/webservices/SharePointPortalServer/UserProfileService'> \
                <AccountName>domain\\username</AccountName> \
            </GetUserProfileByName> \
        </soap:Body> \
        </soap:Envelope>";

    var call = $.ajax({
        url: "http://sharepoint/_vti_bin/UserProfileService.asmx/GetUserProfileByName",
        type: "POST",
        dataType: "xml",
        username: "domain\\username",
        password: "passwd",
        data: soapEnv,
        headers: {
            contentType: "text/xml; charset=\"utf-8\""
        }
    });

Is this the correct approach to accomplish the requirement? if anyone can give a hint on this would be appreciated.

2 Answers 2

0

Try the demo below:

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

Below code will give you logged in user properties:

SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');

var userProfileProperties;

function getUserProperties() {
var clientContext = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
userProfileProperties = peopleManager.getMyProperties();
clientContext.load(userProfileProperties);
clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
}
 function onRequestSuccess() {

 var WorkEmail=userProfileProperties.get_userProfileProperties()['WorkEmail'];
 var Phone=userProfileProperties.get_userProfileProperties()['WorkPhone'];
   }

function onRequestFail(sender, args) { alert( args.get_message());}

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.