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]

2
  • 1
    Are you sure about your list Title ? Fiance or Finance, Verify the restUrl Commented Feb 4, 2019 at 14:03
  • there is no problem, just typo
    – NOOR
    Commented Feb 4, 2019 at 14:09

2 Answers 2

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 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.