0

have 2 lists, I tried to retrieve the values ​​of lookup field (one or more values ​​from the Finance list (Title, Country (lookup field to the list Country column "Title"

$("#Finance").autocomplete({

    minLength: 2,

    source: function(request, response) {

        var term = request.term;

        var restUrl = "/_api/web/lists/GetByTitle('Finance')/items?$select=Title,Pays/Country&$expand=Country/Title&$filter=substringof('" + term + "',Title)";
        $.ajax({

            contentType: "application/json;odata=verbose",

            headers: { "accept": "application/json;odata=verbose" },

            url: restUrl,

            dataType: 'json',
            async: false,
            cache: false,
            beforeSend: function() {
                console.log("beforeSend");
            },
            success: function(data) {

                response($.map(data.d.results, function(value, key) {


                    return {
                        label: value.Title,
                        value: value.Title,
                        country: value.Country,
                        value: value.Country

                    };

                }));

            },
            error: function(data) {
                alert('Erreur');
            },
            complete: function() {
                console.log("complete");
            }
        });

    },

    // Run this when the item is in focused (not selected)
    focus: function(event, ui) {

        return false;
    },
    // Run this when the item is selected
    select: function(event, ui) {
        $("#Country").val(ui.item.label);
        $("#Finance").val(ui.item.Country);
        return false;
    },
});

});

the result is [object] [object], [object] [object]

  • 1
    Are you sure about your list Title ? Fiance or Finance, Verify the restUrl – Ahmed Bouchaala Feb 4 at 14:03
  • there is no problem, just typo – Alice Feb 4 at 14:09
0

I don’t understand what "$select=Title,Pays/Country&$expand=Country/Title" means.

In general, we use $select=Title,LookupField/Id,LookupField/Title,LookupField/CustomField&$expand=LookupField

You need to get lookup value in success function, e.g.

return {
    label: value.Title,
    value: value.Title,
    country: value.Country.Title,
    value: value.Country.Title    
};

Here is my testing :

my lists:

enter image description here

Use the following scripts in the page:

  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
$( function() {
    $("#Finance").autocomplete({

        minLength: 2,

        source: function(request, response) {

            var term = request.term;

            var restUrl = _spPageContextInfo.webAbsoluteUrl +"/_api/web/lists/GetByTitle('Finance')/items?$select=Title,Country/Title,Country/ID&$expand=Country&$filter=substringof('" + term + "',Title)";
            $.ajax({

                contentType: "application/json;odata=verbose",

                headers: { "accept": "application/json;odata=verbose" },

                url: restUrl,

                dataType: 'json',
                async: false,
                cache: false,
                beforeSend: function() {
                    console.log("beforeSend");
                },
                success: function(data) {

                    response($.map(data.d.results, function(value, key) {


                        return {
                            label: value.Title,
                            value: value.Title,
                            country: value.Country.Title,
                            value: value.Country.Title

                        };

                    }));

                },
                error: function(data) {
                    alert('Erreur');
                },
                complete: function() {
                    console.log("complete");
                }
            });

        },

        // Run this when the item is in focused (not selected)
        focus: function(event, ui) {
            return false;
        },
        // Run this when the item is selected
        select: function(event, ui) {
            $("#Finance").val(ui.item.label);
            $("#Country").val(ui.item.country);
            return false;
        },
    });

});
  </script>


<table>
<tr>
<td>
  <label for="Finance">Finance: </label>
  <input id="Finance">
</td>
<td><input id="Country"></td>
</tr>
</table>

The testing results:

enter image description here

Selected "JavaScript":

enter image description here

0

Multi valued lookups come back as arrays, so you need to process them as such.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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