SPServices GetListItems function within another GetListItems function

285 Views Asked by At

I am working on a project that involves two separate lists within the same SharePoint site. I have used the GetListItems function in SPServices to retrieve the list items from list A and plug the vales from specific fields into an HTML string to display on a page.

I have a second list, List B, that I want to be able to query using GetList Items as well, but based on the results from the first query.

List B contains a lookup column based on the ID column of List B. So the two should share a common data point. What I want to do is be able to click on one of the items returned from List A - and view the items from List B that pertain to it based on the lookup column.

I understand this could be more of a Javascript question than an SPServices question, but I was not sure where to start given the specific usage of the GetListItems function. Can anyone give me any insight as to how I might achieve this?

Thanks.

1

There are 1 best solutions below

0
AudioBubble On

The following code uses SPPlus to make life easier:

Purpose: Get items from list A, then figure out, for each item in list A, how many exist in list B with the same title.

<head>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/sharepointplus-5.1.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script>

    $(document).ready(function () {
        ExecuteOrDelayUntilScriptLoaded(onLoad(listA, listB), "sp.js");
    });

    function onLoad(listA, listB) {

        $SP().list(listA).get({
            fields: "Title",
        }).then(function (response) {
            var obj = {};
            var dataA = response;
            dataA.forEach(rowA => {
                let titleFromListA = rowA.getAttribute("Title");
                $SP().list(listB).get({
                    fields: "Title",
                    where: "Title='" + titleFromListA + "'"
                }).then(function (response) {
                    let dataB = response;
                    let countofResultFromB = dataB.length;
                    obj[titleFromListA] = countofResultFromB;
                })
            });
            console.log(obj)
        })
    }
</script> </head>