1

This JavaScript code copies title field of one list to other list.

<script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
    var siteUrl = 'url';
function retrieveListItems() {
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('demo1');

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
        '<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
    this.collListItem = oList.getItems(camlQuery);    
    clientContext.load(collListItem);
       // alert(collListItem);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        

}
var IDArray = [];

function onQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator(); 

    while (listItemEnumerator.moveNext()) {
        var objListItem = listItemEnumerator.get_current();
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('demo2');
        var itemCreateInfo = new SP.ListItemCreationInformation();
        this.oListItem = oList.addItem(itemCreateInfo);
        if(objListItem.get_item('Title')=='z')
{


        oListItem.set_item('Title', objListItem.get_item('Title'));
       // oListItem.set_item('Text', objListItem.get_item('Text'));

        alert(oList);
        oListItem.update();
        clientContext.load(oListItem);
}
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededFinal), Function.createDelegate(this, this.onQueryFailed));
    }
}
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
function onQuerySucceededFinal(sender, args) {
     //Do next set of operation if needed
}
    </script>
<input type="button" value="Submit" onclick="retrieveListItems()" name="btnVarIQReject" />

only title field value is copied, I have more than 100 columns in the list and I have to copy all the columns values to other list. Doing it manually by adding code for each column would not be the right way, is there any other way to do this?

2
  • Do you want to make exact replica of the entire Demo1 list OR Demo2 list already contains some other items and you want to merge Demo1 list items into it ? Commented Aug 21, 2015 at 9:08
  • i want to copy the records with some condition like if status column is completed. Commented Aug 21, 2015 at 10:20

2 Answers 2

2

Basically you need to iterate through the whole list field collection, do checks (if the field is read only, visible etc.) and then assign the value.

Replace this (instead of setting one particular column):

oListItem.set_item('Title', objListItem.get_item('Title'));

with this (iterating through all the columns in a list):

this.listFields = oList.get_fields();
var fieldEnumerator = listFields.getEnumerator();
while (fieldEnumerator.moveNext()) {
    var oField = fieldEnumerator.get_current();
    oListItem.set_item(oField.internalName, objListItem.get_item(oField.internalName));
}
4
  • is there code to copy the record directly without going to each column. Commented Aug 21, 2015 at 10:21
  • Your original question is how to copy all field values from one item to another. And if I had to make a duplicate item that is the way I'd go. Commented Aug 21, 2015 at 10:25
  • I think ,it would be better if we can copy the specific record rather that going to each column. Is it possible? Commented Aug 21, 2015 at 10:28
  • No. You add a new item and you copy the values. First make sure the field is not read only before assigning a new value. Commented Aug 21, 2015 at 13:12
0

Isn't this problem addressed by a workflow in SharePoint Designer?

I'd create a new workflow that:

On update, if Status=Complete, then create a new list item in another list, or update an existing list item in another list.

Why are you stuck with java for this? I appreciate your coding, I'm adapting it to solve my own issues on this side of the electrons, but I don't get why you'd use Java on purpose in the first place when you can make a workflow do your bidding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.