I'm having an issue with this code for pulling usernames from sharepoint.
The function GetuserName()
works fine if i change return loginName to alert(loginName); and then i just call the function.
but i'm trying to pull back the value and add it to the arrVal array.
however it's not working, while testing with alerts if I alert(GetUserName());
I get an undefined alertbox. i'm guessing i'm calling the return back wrong but i'm not sure.
Any help would be really useful
(document).on('click', '.genbutt', function() {
var $row = jQuery(this).closest('tr');
var $columns = $row.find('td');
$columns.addClass('row-highlight');
var values = "";
var arrVal = [];
jQuery.each($columns, function(i, item) {
values = values + item.innerHTML + " ";
arrVal = values.split(" ");
});
alert(GetUserName());
});
function GetUserName() {
var userid = _spPageContextInfo.userId;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
var requestHeaders = {
"accept": "application/json;odata=verbose"
};
$.ajax({
url: requestUri,
contentType: "application/json;odata=verbose",
headers: requestHeaders,
success: onSuccess,
error: onError
});
function onSuccess(data, request) {
var loginName = data.d.Title;
var emailName = _spPageContextInfo.userLoginName;
return loginName;
}
function onError(error) {
alert("error");
}
}