0

I have two lists and I would like to move a list item to another list on a webpart button click. Is there a way to move items using jquery in sharepoint?

Thanks.

  • 1
    Moving items using jQuery will lose the base metadata (created, created by, ...), versioning – JayHell Apr 22 '16 at 15:35
  • even with the answer of @jpussacq? – Jey10 Apr 22 '16 at 22:23
  • why not use ootb content manager sharepoint.stackexchange.com/questions/140457/… or by workflow – user13408 Apr 23 '16 at 6:26
  • thanks, but if I understand, you're telling me to use workflow but I need Jquery to check with a a jquery dynamic count I do. – Jey10 Apr 23 '16 at 9:52
1

You need to use Client Object Model. Look in this link for basic operations:

https://msdn.microsoft.com/en-us/library/office/jj163201.aspx#BasicOps_SPListItemCRUD

Form this links, you have sample to create and delete items(like a move)

Create:

function createListItem(siteUrl) {
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Announcements');

    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
    oListItem.set_item('Title', 'My New Item!');
    oListItem.set_item('Body', 'Hello World!');
    oListItem.update();

    clientContext.load(oListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    );
}

function onQuerySucceeded() {
    alert('Item created: ' + oListItem.get_id());
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}

Delete

function deleteListItem(siteUrl) {
    this.itemId = 2;
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Announcements');
    this.oListItem = oList.getItemById(itemId);
    oListItem.deleteObject();

    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    );
}

function onQuerySucceeded() {
    alert('Item deleted: ' + itemId);
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}
  • Finally I've used Client Object Model with Caml to move specific items to another list. Good way because the changes are recognized by the workflows, so we can work with – Jey10 Apr 27 '16 at 6:49
  • I am glad that the model has served. If you believe the answer is correct, you can mark it. Greetings! – Juan Pablo Pussacq Laborde Apr 27 '16 at 19:07
1

You can use the JavaScript Object Model to create and delete items.

For reference, see Common Programming Tasks in the JavaScript Object Model, the SP Namespace, and SharePoint 2010 Client Object Model Hierarchy and Identity.

Here's an example function to get you started:

function moveItem(sourceItemId){
    var sourceListTitle = "My Source List";
    var destinationListTitle = "My Destination List";
    var clientContext = new SP.ClientContext();
    var sourceList = clientContext.get_web().get_lists().getByTitle(sourceListTitle);
    var oldItem = sourceList.getItemById(sourceItemId);
    clientContext.load(oldItem);
    clientContext.executeQueryAsync(function(){
        var destinationList = clientContext.get_web().get_lists().getByTitle(destinationListTitle);
        var newItem = destinationList.addItem();
        newItem.set_item("Title",oldItem.get_item("Title")); 
        // repeat above line for each field you want to copy
        newItem.update();
        oldItem.deleteObject();
        clientContext.executeQueryAsync(function(){
            alert("Item moved successfully.");
        },function(sender,args){
            alert(args.get_message());
        }); 
    },function(sender,args){
        alert(args.get_message());
    });
}

After you edit that function to use the actual titles of your source and destination lists, you can invoke it by passing it the ID of the source item you want to move. For example: moveItem(32);

  • 1
    Finally I've used Client Object Model with Caml to move specific items to another list. Good way because the changes are recognized by the workflows, so we can work with – Jey10 Apr 27 '16 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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