Implementing #PeopleSearch / Active Directory Users Search in #ProjectOnline

Sometime in your implementation or enhancement to Project Online you might require an search facility for users in active directory like an peoplepicker in SharePoint.

 

Follow the below to implement user search in ProjectOnline.

  1. Add an content webpart editor
  2. Add the below script to webpart. Below in code has <<>> which you’ll have to update to based on your selectors and controls.

<script>

$(document).on("click","<<button selector of search>>",function () {
if($(<<Text field of name>>).val().length <3){
alertify.error("Minimum first/last name to be typed for search");
return;
}
users = [];
userProfileProperties = [];
GLOBAL_SEARCHRESULTDIV = "<<result in div/any selector>>";
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getAllUsers(<<selector of text field search>>));
$(GLOBAL_SEARCHRESULTDIV).show();
$(GLOBAL_SEARCHRESULTDIV).html("Loading... If it's taking longer than expected. Please refresh the page and try again");
});

var users = [];
var userProfileProperties = [];

//Method to fetch all the users

function getAllUsers(searchBox) {

//Textbox value containing search term
var searchTerm = $(searchBox).val();

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 onQueryError(sender, args) {
alert(args.get_message());
}
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"];

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() {

var html = "<style type='text/css'> .floatL {float:left;margin:10px;} .floatR {padding-top:10px} .profile {padding:10px 10px;} .editProfile{margin-left:100px;} div>img {height:72px;width:72px;} </style>";
for (var i = 0; i < userProfileProperties.length; i++) {

html += "<div class='floatR'><a class='selectresource' href='#'><h4><span>" + userProfileProperties[i][0] + "</span></h4></a></div>";

}
$(GLOBAL_SEARCHRESULTDIV).html(html);
$(GLOBAL_SEARCHRESULTDIV).show();
}

</script>