SharePoint Online: Copy List Item using JavaScript
Requirement:
This is very common requirement to copy one or more items from SharePoint list. We can achieve it using Workflow, but the simple solution is to use JavaScript.
Our requirement is to select one or more items in SharePoint List and click on Copy Link, and the same record will be copied in the list.
Solution:
For the above requirement, I have created a List name "Marks" with two columns Title and PercentageScore. Just add a script editor WebPart and paste below code.
Code:
<script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript" src="/_layouts/15/SP.RequestExecutor.js"></script>
<a title="Copy "
class="ms-heroCommandLink ms-hero-command-enabled-alt"
id="idCopy"
onclick='copy()'
href="#">
<span>Copy</span>
</a>
<span id="dvShowMessage"></span>
<script type="text/javascript">
function createListItems(listTitle,itemsProperties,OnItemsAdded,OnItemsError)
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);
var items = [];
$.each(itemsProperties, function (i, itemProperties) {
var itemCreateInfo = new SP.ListItemCreationInformation();
var listItem = list.addItem(itemCreateInfo);
for(var propName in itemProperties) {
listItem.set_item(propName, itemProperties[propName])
}
listItem.update();
context.load(listItem);
items.push(listItem);
});
context.executeQueryAsync(
function() {
OnItemsAdded(items);
},
OnItemsError
);
}
function copy(){
document.getElementById('dvShowMessage').innerHTML = ' Copying...';
var tasksProperties = [];
var context = new SP.ClientContext.get_current();
var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
if(selectedItems.length != 0){
getSelectedItems().then(function(items){
for (var i =0 ; i < items.length;i++){
tasksProperties.push(
{
'Title':items[i].get_item('Title'),
'PercentageScore':items[i].get_item('PercentageScore')
}
);
}
createListItems('Marks',tasksProperties,
function(items){
document.getElementById('dvShowMessage').innerHTML ="";
document.location.reload();
},
function(sender,args){
document.getElementById('dvShowMessage').innerHTML ="<font color ='red'>Error: Please contact administrator.</font>"
}
);
});
}
else{
alert("Please select at least one or more record to copy.");
document.getElementById('dvShowMessage').innerHTML ="<font color ='red'>Error: Please select at one or more to copy.</font>";
return false;
}
}
function getSelectedItems(){
var dfd = $.Deferred(function () {
var lists = [];
var key;
var context = SP.ClientContext.get_current();
var listId = SP.ListOperation.Selection.getSelectedList();
var listContext = context.get_web().get_lists().getById(listId);
var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
for (key in selectedItems){
var item = listContext.getItemById(parseInt(selectedItems[key].id));
lists.push(item);
context.load(item);
}
context.executeQueryAsync(
function() {
dfd.resolve(lists);
},
function (sender, args) {
dfd.reject(args.get_message());
}
);
});
return dfd.promise();
}
</script>
Thanks,
Jayant