0

I want to fetch items of Person or Group type column from another list in React SharePoint but there is a restriction I cannot use it in a single query. So far I have only known using REST. So if anyone has any idea how to use nested calls and filtering the common items between both columns. OR Using another type of query can also be helpful for me.

I am trying to use this way of nested query though and so far it is fetching only from APP_NAME2 but I want common items from both lists:

rows1 and columns1 are the property of DataTable in react-data-table-component.

APP_NAME1 - BID(lookup column in APP_NAME2) and Temperature.

APP_NAME2 - ID, Created By (Field - Author).

private userData = () => {
    $.ajax({
      url: `${this.props.siteUrl}/_api/Web/Lists/GetByTitle('APP_NAME1')/items?$select=*,BookingID/ID&$expand=BookingID`,
      type: "GET",
      dataType: "json",
      headers: {
        accept: "application/json;odata=verbose",
      },
      success: (resultData) => {
        var listdata = {
          accounting: [],
        };
        console.log(resultData.d.results);
        $.each(resultData.d.results, (index, value) => {
            listdata.accounting.push({
              BookingID: value.BookingID.ID,
            });        
        });
        console.log(listdata.accounting);
        $.ajax({
          url: `${this.props.siteUrl}/_api/Web/Lists/GetByTitle('APP_NAME2')/items?$select=Title,Author/ID&$expand=Author&$filter=ID eq + BookingID`,
          type: "GET",
          dataType: "json",
          headers: {
            accept: "application/json;odata=verbose",
          },
          success: (resultData) => {
            var listData= {
              accounting: [],
            };
            console.log(resultData.d.results);
            $.each(resultData.d.results, (index, value) => {
              listData.accounting.push({
                Author: value.Title,
                BookingID: value.Author.ID,
              });
            });
            console.log(listData.accounting);
            this.columns1 = [
              {
                name: "Employee Name",
                selector: "Author",
              },
              {
                name: "ID",
                selector: "BookingID",
              },
            ];
            this.rows1 = listData.accounting;
            this.setState({ healthScreening: listData.accounting }, () => {});
          },
          error: (jqXR, textStatus, errorThrown) => {
            console.log("error");
          },
        })
      },
      error: (jqXHR, textStatus, errorThrown) => {
        console.log("error");
      },
    });
  }

2 Answers 2

1

Based on your last question at: Item not fetching using lookup in React Sharepoint,

You need to make nested REST calls to get the information from another list. Below are general steps you can perform to achieve this:

  1. Make first REST call to get the information related to Booking item:
<siteUrl>/_api/Web/Lists/GetByTitle('APP_NAME')/items?$select=*,BookingID/ID&$expand=BookingID
  1. Then in success function of first call, you can store list item id from booking ID to one variable (say bookingItemId) - like you are doing in above link to your previous question.

  2. Then you can filter 2nd list like:

"<siteUrl>/_api/Web/Lists/GetByTitle('<Second list name>')/items?$select=Title,Author/ID&$expand=Author&$filter=ID eq " + <bookingItemId>

Replace <Second list name> with the name of your list and <bookingItemId> with your variable name.

5
  • So like the first REST call I am calling in a function, then the second REST call will be inside the success function like url: "<siteUrl>/_api/Web/Lists/GetByTitle('<Second list name>')/items?$select=Title,Author/ID&$expand=Author&$filter=ID eq " + <bookingItemId> or I will have to put that in another function or will it be in first REST call function but outside the success function ? Commented Nov 24, 2020 at 8:23
  • You have to write it inside success function of first call (After you get the booking item Id from first call). If you want to make your success function cleaner then you can create a new function with the 2nd REST call and call that function within success function of first REST call. Commented Nov 24, 2020 at 8:45
  • It is not working or I might be doing some mistake. I have edited my ques in more detailed manner. Commented Nov 24, 2020 at 9:12
  • I am not able to see any edit to your question. can you please add your current code to your question? I will have a look at it and might able to tell you where you are going wrong. Commented Nov 24, 2020 at 9:22
  • I was making changes. Sorry for the delay. Please Refresh it again Commented Nov 24, 2020 at 9:32
1

You could call another Rest API in the success function of the first.

private userData = () => {
  $.ajax({
    url: `${this.props.siteUrl}/_api/Web/Lists/GetByTitle('APP_NAME1')/items?$select=*,BookingID/ID&$expand=BookingID`,
    type: "GET",
    dataType: "json",
    headers: {
      accept: "application/json;odata=verbose",
    },
    success: (resultData) => {
      var listdata = {
        accounting: [],
      };
      console.log(resultData.d.results);
      $.each(resultData.d.results, (index, value) => {
          listdata.accounting.push({
            BookingID: value.BookingID.ID,
          });  
          $.ajax({
            url: `${this.props.siteUrl}/_api/Web/Lists/GetByTitle('APP_NAME2')/items?$select=Title,Author/ID&$expand=Author&$filter=ID eq + ${value.BookingID.ID}`,
            type: "GET",
            dataType: "json",
            headers: {
              accept: "application/json;odata=verbose",
            },
            success: (resultData) => {
              var listData= {
                accounting: [],
              };
              console.log(resultData.d.results);
              $.each(resultData.d.results, (index, value) => {
                listData.accounting.push({
                  Author: value.Title,
                  BookingID: value.Author.ID,
                });
              });
              console.log(listData.accounting);
         //I think you need to modify here.
              this.columns1 = [
                {
                  name: "Employee Name",
                  selector: "Author",
                },
                {
                  name: "ID",
                  selector: "BookingID",
                },
              ];
              this.rows1 = listData.accounting;
              this.setState({ healthScreening: listData.accounting }, () => {});
            },
            error: (jqXR, textStatus, errorThrown) => {
              console.log("error");
            },
          })      
      });
      console.log(listdata.accounting);
      
    },
    error: (jqXHR, textStatus, errorThrown) => {
      console.log("error");
    },
  });
}

Because I don’t know the structure of the return value of your request, I can’t modify the values of some variables in the code. I can only modify what I think is wrong.

8
  • this.columns1 is a property in react-data-table-component for displaying field values. The values which are returning are from of APP_NAME2 but I want to fetch common items from APP_NAME1 and APP_NAME2 Commented Nov 25, 2020 at 5:38
  • I mean you need to replace the hard-coded value with a variable value to complete the assignment.
    – Amos
    Commented Nov 25, 2020 at 5:40
  • But I don't think there is a hard coded value. If it is can you please point it out so that I could explain more Commented Nov 25, 2020 at 5:43
  • I just think there may be a problem. If you think it’s okay, then you don’t need to modify the code, just follow your ideas.
    – Amos
    Commented Nov 25, 2020 at 5:48
  • What I am saying is that I am able to fetch the items of APP_NAME2 only but what my requirement is to get common items from 2 API. If you are not able to understand the structure of my code you can also tell me another working method with example Commented Nov 25, 2020 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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