Create new list item with Date Column - UpdateListItems

Jun 16, 2011 at 10:16 PM

I'm trying to move a database of news archives into a SharePoint list.  I have the database exported as an xml file, and jQuery written to parse for use with SPServices.  I'm using UpdateListItems to create a new item in the list based on the parameters from the xml.  

My list has a Date column that accepts a date and time. I'm having a problem creating a new item with this particular column.  If I remove "date" from the parameter list I can create a new list item without any problems.  

My guess is that I need to pass the value of the date through with specific syntax, but I don't know what it is nor can I find it anywhere.  

I'd be grateful for any direction someone can push me in.  I'm not sure if I explained my goal well enough, if not I can describe in more detail.

My jQuery code and the xml file I'm pulling from is below:

$( document ).ready( function() {
  $.ajax( {
    type: "GET",
    url: "../SiteAssets/scripts/news.xml",
    dataType: "xml",
    success: parseXml
  } );
} );

function parseXml( xml ) {
	var title;
	var date;
	var text;
	//find every Tutorial and print the author
	$( xml ).find( "row" ).each( function() {
		
		title 	= $( this ).find( "news_title" ).text();
		date 	= $( this ).find( "news_date" ).text();		
		text 	= $( this ).find( "news_body" ).text();	
		
		CreateNewItem( title, date, text );
	} );
}

function CreateNewItem( title, date, text ) {
    $().SPServices({
        operation: "UpdateListItems",
        async: false,
        batchCmd: "New",
        listName: "news_archive",
        valuepairs: [["Title", title], ["Date", date], ["Text", text], ["Image", "default.jpg"]],
        completefunc: function(xData, Status) {
          alert("completed");
        }
    });
}
<?xml version="1.0" encoding="UTF-8"?> 
<articles>
	<row>
	  <news_id>31</news_id>
	  <news_title>UWSP looks to lessen electronic waste</news_title>
	  <news_body>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vitae elit nibh. 
	  Duis imperdiet felis tempus lectus ultricies a porttitor tortor tristique. Curabitur diam leo, 
	  ultrices a aliquam at, scelerisque at nisl. In vitae nulla in dolor mattis pharetra ac non tellus. 
	  Nunc laoreet eleifend ipsum scelerisque mattis. Pellentesque habitant morbi tristique senectus et 
	  netus et malesuada fames ac turpis egestas. Donec eget eros odio, vel convallis ante. Donec in eros sit 
	  amet magna imperdiet consequat.</news_body>
	  <news_date>2009-01-14T11:11:00</news_date>
	  <news_image>Default</news_image>
	</row>
</articles>

~Drew

Coordinator
Jun 17, 2011 at 12:03 AM

drewfrisk:

I just tested with this format and it worked:

valuepairs: [["Title", now], ["Country", 3], ["Lead_x0020_Date", "2009-08-25 14:24:48"]],

The issue is with the "T".

M.

Jun 17, 2011 at 12:19 AM

Got it! Thanks!

:)

Coordinator
Jun 17, 2011 at 12:22 AM

You bet!

M.

Jun 17, 2011 at 1:17 AM

Another trick I use, is to fill out an OOTB form with all of your values.  Then you can use GetListItems to retrieve that item that you created.  What is returned is a valid format for writing back to the list.

Jun 17, 2011 at 1:59 AM

The script I posted earlier ended up working incredibly well, I just had a problem of passing the Date parameter through with the wrong syntax like Marc suggested.

Worked like a charm once I parsed that "T" out of there!

~Drew