Skip to content

Instantly share code, notes, and snippets.

@VibhuKuchhal
Created May 20, 2016 03:42
Show Gist options
  • Save VibhuKuchhal/b7248a27b829dd6ce8fc49a58680b3d5 to your computer and use it in GitHub Desktop.
Save VibhuKuchhal/b7248a27b829dd6ce8fc49a58680b3d5 to your computer and use it in GitHub Desktop.
function AadPicker(tenant, token, selectedItem) {
var pairedObjectId, pairedEmailId;
var pairedObjectIdId = selectedItem.data("idpairing");
if (pairedObjectIdId) {
pairedObjectId = $("#" + pairedObjectIdId);
}
var pairedEmailIdId = selectedItem.data("emailpairing");
if (pairedEmailIdId) {
pairedEmailId = $("#" + pairedEmailIdId);
}
var graphLoc = "https://graph.windows.net";
var apiVersion = "1.6";
if (selectedItem.data("nature") === "autocomplete") {
selectedItem.bind("keyup", function (e) {
if (selectedItem.val() == "" && pairedObjectId) {
var pairedValue = "";
pairedObjectId.val(pairedValue);
pairedEmailId.val(pairedValue);
}
});
selectedItem
.bind("keydown", function (event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).autocomplete("instance").menu.active) {
event.preventDefault();
}
})
.autocomplete({
minLength: selectedItem.data("minlength"),
source: //available()
function (request, response) {
function ConstructUserQuery(inputValue) {
var url = graphLoc + '/' + tenant + "/users?api-version="
+ apiVersion + "&$top=30";
if (inputValue.length > 0) {
url += "&$filter=" +
"startswith(displayName,'" + inputValue +
"') or startswith(givenName,'" + inputValue +
"') or startswith(surname,'" + inputValue +
"') or startswith(userPrincipalName,'" + inputValue +
"') or startswith(mail,'" + inputValue +
"') or startswith(mailNickname,'" + inputValue +
"') or startswith(jobTitle,'" + inputValue +
"') or startswith(department,'" + inputValue +
"') or startswith(city,'" + inputValue + "')";
}
return url;
}
var getUrl = ConstructUserQuery(request.term);
$.ajax({
url: getUrl,
type: "GET",
headers: {
'Authorization': 'Bearer ' + token,
},
success: function (data) {
console.log(data);
if (data != null && data.value != null) {
response($.map(data.value, function (item) {
return {
label: item.displayName,
value: item.objectId
}
}))
}
}
});
}
,
focus: function () {
// prevent value inserted on focus
return false;
},
select: function (event, ui) {
this.value = ui.item.label;
if (pairedObjectId) {
var pairedValue = ui.item.value;
var getUrl = graphLoc + '/' + tenant + "/users/" + pairedValue + "?api-version=" + apiVersion;
$.ajax({
url: getUrl,
type: "GET",
headers: {
'Authorization': 'Bearer ' + token,
},
success: function (data) {
console.log(data);
if (data != null && data.userPrincipalName != null) {
pairedObjectId.val(pairedValue);
pairedEmailId.val(data.userPrincipalName);
}
}
});
}
return false;
}
});
}
}
function AadInitiator(tenant, token)
{
$('.aadpicker').each(function () {
var selectedItem = $(this);
AadPicker(tenant, token, selectedItem);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment