1

I am having trouble to create an expand query using the REST API.

I have two lists which are linked.
PIR
Title, Date, Number

SIR
Title, PIR2SIR (lookup to Title in PIR), Date, Number
Now I want to display all listitems from PIR with their related SIR's

$.ajax({

    url: url + "/_api/web/lists/getbytitle('" + listname + "')/items?$select=Title,Date, Number,Title/PIR2SIR&$expand=Title",
    method: "GET",                                                      
    headers: { "Accept": "application/json; odata=verbose" },
    timeout: 2000,
    dataType:"json",
    success: function (data) {
        complete(data); // Returns JSON collection of the results
    },
    error: function (xhr,status,error) {
        console.log(status);
    }
});

My code returns an error. How should I use the linked list in this query?

  • What is error your getting? – Sunil Sahu Jul 24 '18 at 10:26
0

You can expand it as

$select=Title,Date, Number,PIR2SIR/Title&$expand=PIR2SIR

This will give you the value of PIR2SIR field.

  • Still get an error with your example. I don't know where it is from. The console just writes error, that's all. – Marco Jul 24 '18 at 11:15
  • I changed the code, now the error I get is the field PIR2SIR doesn't exists. – Marco Jul 24 '18 at 12:11
  • Your select only works when I use the second list (SIR) as source. But then I get the data in the wrong way. SIR 1.1 PIR 1 SIR 1.2 PIR 1 – Marco Jul 24 '18 at 12:25
  • yes, the query will work when you are fetching the data from SIR list since the lookup field is in the SIR list. To display : "all listitems from PIR with their related SIR's" : you can still query the SIR list to get all items which will give you the "PIR2SIR" field (which is the PIR as it is a lookup) and also the corrosponding "title" of SIR. This way you already have a mapping of SIR and PIR...am I missing something ? – Vipul Kelkar Jul 25 '18 at 3:50
  • Well I think items in the PIR list which have no SIR are not displayed for one. Secondly I need some code to display them, like: for(var i =0; i < data.d.results.length; i++) { if (eh != data.d.results[i].Eenheid) { console.log(data.d.results[i].Eenheid); eh = data.d.results[i].Eenheid; } if (PIR != data.d.results[i].PIR2SIR.Title) { console.log(data.d.results[i].PIR2SIR.Title); PIR = data.d.results[i].PIR2SIR.Title; } console.log(data.d.results[i].Volgnr); console.log(data.d.results[i].Title); – Marco Jul 25 '18 at 8:33

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.