0

newbster here to SP and JS...I have a list with a bunch of lookup columns (mostly people pickers). I need to access the data via REST calls, but when i try to grab it I am getting a error saying i can only access a certain number of lookup columns. So I just made two REST calls and thought I'd build my HTML that way. Question is, how do i associate the data from my first REST call and my second so that the HTML builds with data from the same list item? When i call my second for loop to go through the second set of data, it just runs through all the list items and assigns the last value it finds to my variable. Any helpful tips for a beginner? Thanks in advance!

function getCategories(listName){
    var url = _spPageContextInfo.siteServerRelativeUrl+
        "/_api/lists/getbytitle('myList')/items?$select=ID,One/Title,One/EMail,One/JobTitle&$expand=One";
    var urlTwo = _spPageContextInfo.siteServerRelativeUrl+
        "/_api/lists/getbytitle('myList')/items?$select=ID,Two/Title,Two/EMail,Two/JobTitle&$expand=Two";
    $.ajax({
        url: url,
        method: 'GET',
        beforeSend: function (XMLHttpRequest){
            XMLHttpRequest.setRequestHeader('Accept', 'application/json; odata=verbose');
            },

                success: function( data ) { 
                     $.ajax({
                         url: urlTwo,
                         method: "GET",
                         headers: {"accept": "application/json;odata=verbose"},
                         success: createHTML(data,dataTwo);

                     }); 
                }   
        });
}
function createHTML(data,dataTwo){
    var arr = data.d.results;
    var arrTwo = dataTwo.d.results;
    var oneName = "";
    var twoName = "";

    for(var i in arr){
        oneName = arr[i].One["Title"];
        for(var l in arrTwo){
            if(arr[i].ID === arrTwo[l].ID){
                twoName = arrTwo[l].Two["Title];
            }
        }
        pendingContainer.append(
            '<div>'+arr[i].One[Title]+'</div>'+
            '<div>'+arr[l].Two[Title]+</div>'
        );
    }
}
1

Either move the HTML update code into the IF:

for(var i in arr){
    oneName = arr[i].One["Title"];
    for(var l in arrTwo){
        if(arr[i].ID === arrTwo[l].ID){
            twoName = arrTwo[l].Two["Title];

            pendingContainer.append(
                '<div>'+arr[i].One[Title]+'</div>'+
                '<div>'+arr[l].Two[Title]+'</div>'
            );

        }
    }
}

Or better, use the two variables you created:

for(var i in arr){
    oneName = arr[i].One["Title"];
    for(var l in arrTwo){
        if(arr[i].ID === arrTwo[l].ID){
            twoName = arrTwo[l].Two["Title];
        }
    }
    pendingContainer.append(
        '<div>'+ oneName +'</div>'+
        '<div>'+ twoName +'</div>'
    );
}

Also, you were missing a quote in front of the last .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.