Skip to main content
Question Protected by CommunityBot
Tweeted twitter.com/StackSharePoint/status/733734769935187968
edited title
Link

Copy List items using RestREST/JSON

Source Link

Copy List items using Rest/JSON

I'm very new to REST and JSON, I want to copy list items from one list to another list in the same site on button click, both the lists are of same type but with different column names and they are 'Tasks' lists. I have the code to get the list items from a list and its below,

// Getting list items based on ODATA Query
function getListItems(url, listname, query, complete, failure) {

    // Executing our items via an ajax request
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items" + query,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            complete(data); // Returns JSON collection of the results
        },
        error: function (data) {
            failure(data);
        }
    });

}

I have the code to create an item in the list and its as below,

    function createListItem(webUrl,listName, itemProperties, success, failure) 
{    
 $.ajax(
{       
 url: webUrl + "/_vti_bin/listdata.svc/" + listName,   
     type: "POST",   
     processData: false,  
      contentType: "application/json;odata=verbose",
        data: JSON.stringify(itemProperties),
        headers: 
{   
         "Accept": "application/json;odata=verbose"
        }, 
       success: function (data) 
{           
 success(data.d);   
     },      
  error: function (data)
 {            
failure(data.responseJSON.error);    
    }  
  });
} 
 //Usage: create taskvar taskProperties = 
{   
 'TaskName': 'Order Approval',    'AssignedToId': 12};
 createListItem('https://contoso.sharepoint.com/project/','Tasks',taskProperties,function(task){ 
   console.log('Task' + task.TaskName + ' has been created');  
 }, 
 function(error){ 
   console.log(JSON.stringify(error)); 
 }); 

Now how to pass the required feild values from getListItems funtion to createListItem function, and how to make all these work on button click?