1

This question already has an answer here:

I am trying to query sharepoint list which is having Lookup columns.

So to do so I am using below JavaScript function

function getDataWithCaml(oDataUrl, caml) {
    var requestData = {
        "query":
               {
                    "__metadata": { "type": "SP.CamlQuery" },
                    "ViewXml": caml
               }
    };
    jQuery.ajax({
        url: oDataUrl,
        method: "POST",
        data: JSON.stringify(requestData),
        headers: {
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "Accept": "application/json; odata=verbose",
            "Content-Type": "application/json; odata=verbose"
        },
        success: function (data, object) {
            getDataWithCaml_Success(data, object);
        },
        error: getDataWithCaml_error
    });
}

And after doing some google I found out that I have to use $expand parameter to do so.

So this is how I am calling this function.

var oDataUrl = "http://URL/_api/Web/Lists(guid'LISTGUID')/GetItems?$select=LOOKUPLIST/LOOKUPLISTFIELDNAME&$expand=LOOKUPLISTFIELDNAME"
var caml = "CAMLQUERY"

When I run this i am getting below error.

The $expand query is not valid for field 'LookupListFieldName'.

marked as duplicate by Waqas Sarwar MVP, wjervis, Eric Alexander Sep 17 '15 at 13:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

To get a lookup field with REST you have to call it like this:

/_api/web/lists/getbytitle('[ListName]')/items(id_item)?$select=Id,Hallazgo/Id&$expand=Hallazgo

Where:

select: here you would indicate the fields you want to show, if they are lookup you have to call it with ColumnName/field, like in my example "Hallazgo/Id"

expand: lookup fields you want extend, you have to use the column name in the list, in my example "Hallazgo"

In case you need use caml the call should go like this:

jQuery.ajax({
    url: thaUrl_with_expand_fields,
    headers: { Accept: "application/json;odata=verbose" },
    body: {
        "query" : {
          "__metadata": {
            "type": "SP.CamlQuery"
          },
          "ViewXml": "<View>" +
            "<Query>" + [query] + "</Query>" +                      
          "</View>"
        }
    },
    success: function (data) { },
    error: function (error) { }
});

Build your query with some tool, just for no mistakes, and replace the [query]

greetings :)

  • Thanks for your response. You may not use CAML with rest but I am using CAML because my query is so complex and might exceed url limit if I use $filter. Also I would like to highlight that if you take a look at my code then you will see that I am using POST method and the one you suggested is GET and CAML will only work with POST. or there might be a way that I do not know. – Ravi Khambhati Sep 16 '15 at 19:51
  • I'm sorry, you did not mention about the complex search. The search methos support POST and GET method. There is no problem with that, just change it. I will edith my post – Marco Sep 16 '15 at 19:59
  • Thanks @Marco. its working but partially. CAML is not being considered with the URL(getbytitle()/items) you mentioned with GET request. URL(getitems) that i mentioned in my question works with CAML but its not returning Lookup Column value. – Ravi Khambhati Sep 17 '15 at 13:36
  • @RaviKhambati you need to set viewfields in your caml to return lookup column. But that will only be returned as lookup id not lookup name. – Akhoy Sep 17 '15 at 17:23

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