I want to copy one item from a list (source) with some of its fields to an identical list (i.e. has the same fields) on another SharePoint site (destination). I've got this far with the code that works.
var listName = "Package"; //same name at source and destination
//Retrieve source info
var sourceCtx = SP.ClientContext.get_current();
var sourceList = sourceCtx.get_web().get_lists().getByTitle(listName);
var camlQuery = new SP.CamlQuery();
self.sourceListItems = sourceList.getItems(camlQuery); //get all list items
sourceCtx.load(sourceListItems, 'Include(File_x0020_Number, Title, Aim, Synopsis)');
sourceCtx.executeQueryAsync(onRetrieveSucceeded, onRetrieveFailed);
function onRetrieveSucceeded() {
console.log('Items retrieved successfully!');
}
function onRetrieveFailed(sender, args) {
console.log('Retrieve items failed.' + args.get_message() + '\n' + args.get_stackTrace());
}
//the specific item to be copied
var sourceListId = getQueryStringParameterByName("ID"); //ID is the number of the current item
var sourceListItem = sourceList.getItemById(sourceListId); //returns a single item from the list
//Info for destination List
var destUrl = <<URL for destination SharePoint site>>;
var destCtx = new SP.ClientContext(destUrl);
var destList = destCtx.get_web().get_lists().getByTitle(listName);
destCtx.load(destList);
//add a new item to the destination list
var newListItem = new SP.ListItemCreationInformation();
destListItem = destList.addItem(newListItem);
destListItem.set_item('File_x0020_Number', '1100-1');
destListItem.set_item('Title', 'This code works!');
destListItem.set_item('Aim', 'To copy fields from source item');
destListItem.set_item('Synopsis', 'blah, blah, blah');
destListItem.update();
destCtx.load(destListItem);
destCtx.executeQueryAsync(onQuerySucceeded, onQueryFailed);
function onQuerySucceeded() {
console.log('Item copied!');
}
function onQueryFailed(sender, args) {
console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
The problem is that I want code that works something like this:
destListItem.set_item('Title', sourceListItem.get_item('Title'));
instead of:
destListItem.set_item('Title', "This code works!");
My knowledge is pretty limited as I'm starting out. I've tried using fieldCollections or Array but just can't get anything to work.