As the title says, I'm trying to get items from a list that are selected by the user to copy over into another list, "Shopping Cart". During the copy, I would also like to add a username field to each item, so that the Shopping Cart list can be filtered according to username and display their own chosen items.
My problems are getting the correct ID's to show when transferring. Whenever I run the code, it creates the right # of items that were selected, but it names every single copied item as the same (always the last item that was selected to be copied prior to running the code).
I have also been struggling with iterating over each of the copied items' fields and setting the new item's fields as those. For now, I have just been trying to set the title correctly before I get too far.
<input name="btnSelectAll" onclick="SelectAll()" type="button" value="Select All"/>
<input name="btnDeselectAll" onclick="DeselectAll()" type="button" value="Deselect All"/>
<input name="btnApprove" onclick="AddToCart()" type="button" value="Add To Cart"/>
<script type="text/javascript">
function SelectAll() {
DeselectAll();
for(var i=0;i<ctx.ListData.Row.length;i++) {
SelectRowByIndex(ctx,i,true);
}
}
function DeselectAll() {
var clvp = ctx.clvp;
var tab = clvp.tab;
DeselectAllItems(ctx,tab.rows,false);
}
function AddToCart() {
var context = SP.ClientContext.get_current();
var siteUrl = 'url';
var clientContext = new SP.ClientContext(siteUrl);
var web = context.get_web();
context.load(web);
var sourceId = SP.ListOperation.Selection.getSelectedList();
var source = web.get_lists().getById(sourceId);
var oList = context.get_web().get_lists().getByTitle('Shopping Cart');
context.load(oList);
var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
var i;
for (i in selectedItems) {
var currentItem = source.getItemById(selectedItems[i].id);
context.load(currentItem);
}
context.executeQueryAsync(Function.createDelegate(this,test),Function.createDelegate(this,error));
function test(sender, args) {
var itemCreateInfo = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', currentItem.get_item('Title'));
oListItem.update();
oList.update();
context.executeQueryAsync(success);
}
function success(sender, args) {
alert('all gravy!');
}
function error(sender, args) {
alert('error');
}
}
</script>