0
votes

I have two lists :one has look up field to the other when i use code below to insert listitem it just insert value in title field and doesn't insert in the look up value i have two fields called :'title' and 'myLookup'

$.ajax({
        url: myurl+"/_api/web/lists/GetByTitle('mylist')/items",
        type: "POST",
        data: JSON.stringify({
            __metadata: { "type": 'SP.Data.mylistListItem' },
            Title: "value",
            'myLookupId': 34 //existing id,

        }),
        headers: {
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (d) {
            alert("Items Inserted!");
            //window.location.href=referrer;    
        },
        error: function (err, result) {
1
what is lookup column name?Negi Rox
i call it "Lookup"Shams Eldeen
is 34 is existing item ?Negi Rox
yes it is a n existing idShams Eldeen
i think "lookup" is predefined may be this is the reason. create another column and make it lookup this time just change the name of your columnNegi Rox

1 Answers

0
votes

Generally For lookup type column, the value will be Integer and must be the ID of Lookup item. In the request body, you have to specified it as InternalNameOfTheColumn + Id

means if your column is AssignedTo then it shouldbe AssignedToId

JSON.stringify({ '__metadata': { 'type': 'SP.Data.mylistListItem' }, 
  'Title': "Hello",'LookupId':34}) // 34 should be existing item.