0

I need to loop through the z:row of a Data View Web Part on the client using JavaScript to push to an array and then use the JavaScript CSOM to create a new list item in an existing list.

var siteUrl='/sites/theSite';
//function called from a button clicked event                       
function createListItem()
{
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('TheList');
var itemCreateInfo = new SP.ListItemCreationInformation();
var itemCreateInfo = new SP.ListItemCreationInformation();

//here is the mess I've gotten into             
function getZ(xData, Status)
    {
    $(xData.responseXML).find("[nodeName=z:row]").each(function()
        {
            var newTitle = $(this).attr("ows_Title");
            this.oListItem = oList.addItem(itemCreateInfo);
            //creates the list item
            oListItem.set_item('Title', newTitle.value);

            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());
}


</script>

marked as duplicate by Eric Alexander, user2536, Robert Lindgren, Vardhaman Deshpande, RJ Cuthbertson Oct 14 '13 at 18:31

This question was marked as an exact duplicate of an existing question.

1

You're mixing two APIs here. The first section of your code is using CSOM and the middle section is more like what you would do with SPServices to process results from a SOAP call. You should choose which access method you want to use and not mix the two. This is why your script is failing, though you don't say what effect you see when it runs.

DVWPs are rendered into markup on the server, which processes the XML. By the time the data gets to the page, there's no z:row stuff.

  • Marc .. I have moved completely to SPServices for this. The challenge in this case was bringing in list data from site collection A and creating list items in site collection B. CSOM won't do this because of XSS - I was trying to get the data into the DOM so I could use the CSOM to create the list items. Thank you so much for making this possible with SPServices! I appreciate your work more than ever! – Greg Oct 14 '13 at 13:08
  • Glad you got it working! – Marc D Anderson Oct 15 '13 at 1:13
0

Please find the working code. I hope this helps you .

function GetItemsFromList()
{
 var liHtml = new Array();
 var i = 0;
 var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('TheList');
function (xData, Status) {
$(xData.responseXML).SPFilterNode("z:row").each(function() {     
i= i+1;
liHtml[i] = $(this).attr("ows_Title") ;         
}
});
for (var j=0;j<liHtml.length ; j++ )
{  
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);        
oListItem.set_item('Title', liHtml[j]);   
oListItem.update();
clientContext.load(oListItem);        
clientContext.executeQueryAsync(Function.createDelegate(this, this.onCreateQuerySucceeded), Function.createDelegate(this, this.onCreateQueryFailed));
}
}

function onCreateQuerySucceeded() {

alert('Item created: ' + oListItem.get_id());
}
function onCreateQueryFailed(sender, args) {

alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
 }
  • Harshini ... The problem is that z:row (SOAP results) disappear in the DOM - I have had to do this a different way. Thank you for your help! – Greg Oct 14 '13 at 13:10

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