Copy Contact Item

Nov 4, 2014 at 3:18 AM
I feel like I've looked everywhere but I can't seem to find a way to use SPServices to copy a Contact item from one Contact list to other Contact list! Sorry in advance if this is a noob question.
Coordinator
Nov 4, 2014 at 4:57 AM
You'll use GetListItems to read the contacts from the source list and UpdateListItems to write the contacts into the destination list.

M.
Nov 7, 2014 at 2:53 AM
I'm still having quiet a bit of trouble with this. Yes, I understand that I have an outer "GetListItems", and an inner "UpdateListItems" for each of the outer list items. No problem there. Where I am coming unstuck is it seems that for each item I have to build up a value/pair array. How do I just save the item retrieved from GetListItems as an object, and then just pass it to UpdateListItems.
Coordinator
Nov 7, 2014 at 7:15 PM
Edited Nov 7, 2014 at 7:17 PM
It's going to be a little harder than that, but not a lot. The reason is that GetListItems returns the data in one format and UpdateListItems needs it a little differently,

Here are the bones, which hopefully can get you rolling. I'm typing this straight out on a plane with no testing.)
var p = $(),SPServices({
  operation: "GetListItems",
  listName: "List 1",
  CAMLViewFields: "<FieldRef Name='Title'/>",
]);
p.done(function() {

  p.responseXML.SPFilterNode("z:row").each(function() {
    $().SPServices({
      operation: "UpdateListItems",
      listName: "List 2",
      batchCmd: "New",
      valuepairs: [["Title", $(this).attr("ows_Title"]]
    });
  });

});
M.
Nov 8, 2014 at 2:51 AM
Hi Marc,
Thank you so much for your help. From your lead, I got to the following as my final solution:
               CAMLQuery = "<Query><Where><In><FieldRef Name='ID' /><Values>";
                for (i = 0; i < rolecontactids.length; i++) { CAMLQuery += "<Value Type='Counter'>" + rolecontactids[i] + "</Value>"; }
                CAMLQuery += "</Values></In></Where></Query>";
                console.log("CAMLQuery: " + CAMLQuery);

                $().SPServices({
                    operation: "GetListItems",
                    webURL: "http://xxxxx/",
                    listName: "All Project Contacts",
                    async: false,
                    CAMLQuery: CAMLQuery,
                    completefunc: function (xData, Status) {
                        console.log(Status + " " + $(xData.responseXML).SPFilterNode("z:row").length);
                        $(xData.responseXML).SPFilterNode("z:row").each(function () {
                            // Get all of the fields in the Destination list
                            // This will vary depending on the version
                            $().SPServices({
                                operation: "GetList",
                                webURL: "http://xxxxx/" + roleItemRef,
                                async: false,
                                listName: "Project Directory",
                                completefunc: function (xData, Status) { // Called when GetList is complete 
                                    contactCAMLViewFields = [];     // Get ALL of the fields defined in the list (as we dont know which ones might be referenced in the Template)
                                    $(xData.responseXML).find("Fields > Field").each(function () { contactCAMLViewFields.push($(this).attr("StaticName")); });
                                    console.log(contactCAMLViewFields);
                                }
                            });

                            // Copy fields from Source list to Target list (check the Target fields actually exist!)
                            var valuePairs = [];
                            if ("Trades" in contactCAMLViewFields) valuePairs.push(["Trade", BlankIfNoValue($(this).attr("ows_Trades"))]);
                            if (inArray("AlertT", contactCAMLViewFields)) valuePairs.push(["AlertT", $('#chkAlertT').prop('checked')]);
                            if (inArray("AlertC", contactCAMLViewFields)) valuePairs.push(["AlertC", $("#chkAlertC").prop('checked')]);
                            if (inArray("AlertD", contactCAMLViewFields)) valuePairs.push(["AlertD", $("#chkAlertD").prop('checked')]);
                            if (inArray("AlertE", contactCAMLViewFields)) valuePairs.push(["AlertE", $("#chkAlertE").prop('checked')]); 

                            valuePairs.push(["Company", BlankIfNoValue($(this).attr("ows_Company"))]);
                            valuePairs.push(["CellPhone", BlankIfNoValue($(this).attr("ows_CellPhone"))]);
                            valuePairs.push(["WorkCity", BlankIfNoValue($(this).attr("ows_WorkCity"))]);
                            valuePairs.push(["WorkPhone", BlankIfNoValue($(this).attr("ows_WorkPhone"))]);
                            valuePairs.push(["Email", BlankIfNoValue($(this).attr("ows_Email"))]);
                            valuePairs.push(["FirstName", BlankIfNoValue($(this).attr("ows_FirstName"))]);
                            valuePairs.push(["Title", BlankIfNoValue($(this).attr("ows_Title"))]);
                            valuePairs.push(["Role", $('#role').val()]);
                            
                            console.log(valuePairs);

                            $().SPServices({
                                operation: "UpdateListItems",
                                webURL: "http://xxxxx/" + roleItemRef,
                                listName: "Project Directory",
                                batchCmd: "New",
                                valuepairs: valuePairs,
                                completefunc: function (xData, Status) {
                                    console.log("Status: " + Status);
                                }

                            });
                        });
                    }
                });