Search User - JackHu88/Comm GitHub Wiki

PnP

$pnp.sp.profiles.getPropertiesFor(loginName).then(function(result) {
    var props = result.UserProfileProperties.results;
    var manager=af.getProperty(props,"Key","Manager");
    manager=manager[0].Value;
    console.log(manager);
    $pnp.sp.web.ensureUser(manager).then(function (user)
    {
        var u = user;
        var userId= user.data.Id;
    });
}).catch(function(err) {
    af.error("Submission failed: " + err);
});

**JSOM - **

var users = [];
var userProfileProperties = [];
function getAllUsers(searchTerm) {
    clientContext = new SP.ClientContext.get_current();
    //Building Keyword query for the search
    var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext);
    keywordQuery.set_queryText(searchTerm);
    keywordQuery.set_sourceId("B09A7990-05EA-4AF9-81EF-EDFAB16C4E31");
    keywordQuery.set_rowLimit(500);
    keywordQuery.set_trimDuplicates(false);

    var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext);
    results = searchExecutor.executeQuery(keywordQuery);

    clientContext.executeQueryAsync(onQuerySuccess, onQueryError);
}
function onQuerySuccess() {
   $.each(results.m_value.ResultTables[0].ResultRows, function () {
        users.push(this.AccountName);
    });
    fetchProfilePropertiesForUsers(); 
}

function fetchProfilePropertiesForUsers() {
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    var profilePropertyNames = ["PreferredName", "PictureURL", "AboutMe", "TechNetProfile", "AccountName","Manager"];
    for (var i = 0; i < users.length; i++) {
        var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(clientContext, users[i], profilePropertyNames);
        userProfileProperties[i] = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
    }
    clientContext.executeQueryAsync(onSuccess, onQueryError);
}

function onSuccess() {
    console.log(userProfileProperties);
}
function onQueryError(sender, args) {
    alert(args.get_message());
}