Get all user names instead of User ID from a List using Rest API SharePoint 2013
Use the below code to get user name directly from a list instead of getting a user ID.
Using $expand OData operator you can specify that the request returns projected fields from other lists and the values of lookups.
function getAllUserNames()
{
var deferred = $.Deferred();
var arrayContractOwner = new Array(); // this array to get all user names from list
var serverUrl = _spPageContextInfo.webAbsoluteUrl;
var ListNameUrl = String.format("{0}/_api/web/lists/getbytitle('ListName')/items?$select=UserColumnName/Name,UserColumnName/Title&$expand=UserColumnName/Id",serverUrl);
$.ajax({
url: ListNameUrl,
type: "GET",
headers: {"Accept": "application/json;odata=verbose"},
cache:false,
success: function(data)
{
var test = data;
$(data.d.results).each(function(){
var UserName = this.UserColumnName.Title;
if($.inArray(UserName, arrayContractOwner)==-1)//check userName not present in this array & insert unique names
{
arrayContractOwner.push(UserName);
}
});
deferred.resolve();
},
error: function(err)
{
alert("Request failed :"+JSON.stringify(err));
}
});
return deferred.promise();
}
Please refer this link for more details Link1
Using $expand OData operator you can specify that the request returns projected fields from other lists and the values of lookups.
function getAllUserNames()
{
var deferred = $.Deferred();
var arrayContractOwner = new Array(); // this array to get all user names from list
var serverUrl = _spPageContextInfo.webAbsoluteUrl;
var ListNameUrl = String.format("{0}/_api/web/lists/getbytitle('ListName')/items?$select=UserColumnName/Name,UserColumnName/Title&$expand=UserColumnName/Id",serverUrl);
$.ajax({
url: ListNameUrl,
type: "GET",
headers: {"Accept": "application/json;odata=verbose"},
cache:false,
success: function(data)
{
var test = data;
$(data.d.results).each(function(){
var UserName = this.UserColumnName.Title;
if($.inArray(UserName, arrayContractOwner)==-1)//check userName not present in this array & insert unique names
{
arrayContractOwner.push(UserName);
}
});
deferred.resolve();
},
error: function(err)
{
alert("Request failed :"+JSON.stringify(err));
}
});
return deferred.promise();
}
Please refer this link for more details Link1
Comments
Post a Comment