1

This question already has an answer here:

The below code doesn't work just because of the lookup column. It is unable to insert items to the list..

<script type="text/javascript">
$(document).ready(function () {
 $('#submit1').click(function(){
var idCount=0;
var filtervalue='';
var array=[1,2,3];

    var filterById = getFilter(array);                  
    function getFilter(ids) {
    var idFilter = "(ID eq ";
    var filter = "";
    for (var i = 0; i < ids.length; i++) {
        if (i == ids.length - 1) {
            filter += idFilter + ids[i]+")";
        } else {
            filter += idFilter + ids[i] + ")"+ "or";
        }
    }
    return filter;
}                           
            $.ajax({
                url:_spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Features')/Items?$filter="+filterById+"&$select=Title,Subsystem/Title&$expand=Subsystem/Title",
         type: "GET",    
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {
for(var k=0;k<array.length;k++)
{

$.ajax({
        url: "https://example.com/teams/_api/web/lists/GetByTitle('Features')/Items",
        type: "POST",
        data:  JSON.stringify({ '__metadata': { 'type': 'SP.Data.FeaturesListItem' }, 
  'Title': data.d.results[k].Title,'Subsystem': data.d.results[k].Subsystem}),
        headers: { 
            "accept": "application/json;odata=verbose",
            "content-type":"application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function(d) {
        alert("Items Inserted!"); 
        },
        error: function() {alert("Failed to Insert Items!");}
});
}
       },
        error: function (error) {
        alert("error");     
        }
    }); 
})

});
</script>

What should I do for this to work?

marked as duplicate by Waqas Sarwar MVP, Eric Alexander Nov 17 '15 at 16:55

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.

  • what is the internal name of the Subsystem? – Atish Dipongkor - MVP Nov 16 '15 at 5:07
  • Its same "Subsystem" – KumarV Nov 16 '15 at 5:13
  • 1
    in GET request change the url like "/_api/web/lists/GetByTitle('Features')/Items?$filter="+filterById+"&$select=Title,Subsystem/ID,Subsystem/Title&$expand=Subsystem. In the POST request change data like JSON.stringify({ '__metadata': { 'type': 'SP.Data.FeaturesListItem' }, 'Title': data.d.results[k].Title,'SubsystemId': data.d.results[k].Subsystem.ID}). and feel welcome to give feedback on my last answers – Atish Dipongkor - MVP Nov 16 '15 at 5:44
  • Is it same for people and group column also? – KumarV Nov 16 '15 at 5:59
  • 1
    Added as answer. If you People or Group column does not allow multiple selection, then it is the same. If it allows multiple selection, then it is different. You have to specify array of ID. You will find everything here codeproject.com/Articles/990131/… – Atish Dipongkor - MVP Nov 16 '15 at 6:21
2

You need the lookup item's ID to save new item. Need to change in the following blocks

In the GET request change the URL

 url:_spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Features')/Items?$filter="+filterById+"&$select=Title,Subsystem/Title,Subsystem/ID&$expand=Subsystem"

In the POST request, change the request body

JSON.stringify({ '__metadata': { 'type': 'SP.Data.FeaturesListItem' }, 
  'Title': data.d.results[k].Title,'SubsystemId': data.d.results[k].Subsystem.ID})

So why do you need to change? Read this for explanation.

Modified code looks like following

<script type="text/javascript">
$(document).ready(function () {
 $('#submit1').click(function(){
var idCount=0;
var filtervalue='';
var array=[1,2,3];

    var filterById = getFilter(array);                  
    function getFilter(ids) {
    var idFilter = "(ID eq ";
    var filter = "";
    for (var i = 0; i < ids.length; i++) {
        if (i == ids.length - 1) {
            filter += idFilter + ids[i]+")";
        } else {
            filter += idFilter + ids[i] + ")"+ "or";
        }
    }
    return filter;
}                           
            $.ajax({
                url:_spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Features')/Items?$filter="+filterById+"&$select=Title,Subsystem/Title,Subsystem/ID&$expand=Subsystem",
         type: "GET",    
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {
for(var k=0;k<array.length;k++)
{

$.ajax({
        url: "https://example.com/teams/_api/web/lists/GetByTitle('Features')/Items",
        type: "POST",
        data:  JSON.stringify({ '__metadata': { 'type': 'SP.Data.FeaturesListItem' }, 
  'Title': data.d.results[k].Title,'Subsystem': data.d.results[k].Subsystem.ID}),
        headers: { 
            "accept": "application/json;odata=verbose",
            "content-type":"application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function(d) {
        alert("Items Inserted!"); 
        },
        error: function() {alert("Failed to Insert Items!");}
});
}
       },
        error: function (error) {
        alert("error");     
        }
    }); 
})

});
</script>

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