I was hoping that someone may be able to give me a pointer on how to achieve the following.
Currently, I have:
A JS function: A jQuery AJAX query that will get items from a SharePoint custom list.
A JS function: it will put the list results in to a datatable.
-
A JS function, that can turn an InitatorId into the real name of a user (GetDisplayName).
function GetDisplayName(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 : GetDisplayNameSuccess, error : GetDisplayNameFail }); } function populateMyOutstandingItems() { SP.SOD.executeFunc("sp.js", "SP.ClientContext", function() { SP.SOD.executeFunc("sp.runtime.js", "SP.ClientContext", function() { var context = SP.ClientContext.get_current(); var queryUrl = "https://companyname.sharepoint.com/subsite/_api/Web/Lists/getbytitle('Read%20Requests')/GetItems"; //change the Url var camlXML = "<Query><Where><And><Eq><FieldRef Name='ReaderId' LookupId='True' /><Value Type='Integer'>" + _spPageContextInfo.userId + "</Value></Eq><Neq><FieldRef Name='Read' /><Value Type='Text'>YES</Value></Neq></And></Where></Query>" var requestData = { "query" : {"__metadata": { "type": "SP.CamlQuery" } , "ViewXml": camlXML } }; return jQuery.ajax({ url: queryUrl, type: "POST", data: JSON.stringify(requestData), headers: { "X-RequestDigest": $("#__REQUESTDIGEST").val(), "Accept": "application/json; odata=verbose", "Content-Type": "application/json; odata=verbose" }, success: onQuerySuccessMyOutstandingItems, error: onQueryErrorMyOutstandingItems }); }); }); } function onQuerySuccessMyOutstandingItems(data) { var jsonData = data.d.results console.log(jsonData) $("#resultsDivMyOutstandingItems").dataTable( { "data" : jsonData, columns: [ { data: "Document_x0020_Name" }, { data: "Document_x0020_Library_x0020_Nam" }, { data: "Request_x0020_Made_x0020_Date" }, { data: "InitatorId" }, { data: "Request_x0020_ReadBy_x0020_Date" }, ], order: [[3, "asc"]] } ); }
At the moment, in the datatable, I have the InitatorID (ie. 30) but I would like to have the real name of the user in the table.
I understand some type of recursive AJAX query will be needed, but I'm not sure what the best way forward would be to achieve this, and was hoping that someone would be able to point me in the right direction.
After reading a bit about chained requests, I tried the code below, but it's wrong I guess, since Chrome complains of an 'unexpected ending':
var request = $.ajax({
url: queryUrl,
type: "POST",
data: JSON.stringify(requestData),
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"Accept": "application/json; odata=verbose",
"Content-Type": "application/json; odata=verbose"
}
}),
chained = request.then(function( data ) {
return $.ajax( spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + data.InitatorId + ")",
{ data: { user: data.Title }} )
});
chained.done(function( data ) {
console.log(data)
});
In short, what is the best to achieve what I'm trying to do?
Many thanks Glen