Need Help Using UpdateListItems

Mar 20, 2012 at 5:23 PM

Hi, 

I've never used UpdateListItems before,  so I need some help. I want to do something very simple: I need to update a column named 'Current Date' with the current date (which I have stored in a variable defined earlier). 

This is what I have from reading and it's totally wrong. Any help would be very appreciated. 

$().SPServices({
    operation: 'UpdateListItems',
    async: false,
    listName: '{AE651F79-E723-4534-9DE1-6396BF431DEC}',
    updates: '<Batch OnError="Continue">' + 
    '<Method ID="1" Cmd="Update">' + 
    '<Field Name="Current_x0020_Date">' + currentDate + 
    '</Field>'    + 
    '</Method>' + 
    '</Batch>',
    completefunc: function(xData, status){
	        //...
    }
});

})

Mar 20, 2012 at 7:05 PM

Reading over the docs:

Using a GUID for the listName does NOT change the context as it does with the Object Model, so you need to use the webURL option as well if your list is in a different site.

Does the list reside in a different site?  Also can you add two things to the completefunc?  This will give a little bit more information, not much, but just a little bit more about what may be going wrong.

$().SPServices({
	operation: 'UpdateListItems',
	async: false,
	listName: '{AE651F79-E723-4534-9DE1-6396BF431DEC}',
	updates: 	'<Batch OnError="Continue"><Method ID="1" Cmd="Update"><Field Name="Current_x0020_Date">' + currentDate + '</Field></Method></Batch>',
	completefunc: function(xData, status){
		console.log( status );
		console.log( xData.responseText );
		//...
	}
});

Cheers,

Mar 20, 2012 at 7:25 PM

Ok, status returned 'success', however xData.responseText returned this: 

<?xml version="1.0" encoding="utf-8"?><soap:Envelope 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body>
<UpdateListItemsResponse 
xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<UpdateListItemsResult><Results><Result ID="1,Update">
<ErrorCode>0x8102000a</ErrorCode><ErrorText>Invalid URL 
Parameter. The URL provided contains an invalid Command or Value.
 Please check the URL again.</ErrorText></Result></Results>
</UpdateListItemsResult></UpdateListItemsResponse>
</soap:Body></soap:Envelope>

Any ideas? 

Mar 20, 2012 at 8:46 PM

Yes, 

Can you post this code's output?

console.log('<Batch OnError="Continue"><Method ID="1" Cmd="Update"><Field Name="Current_x0020_Date">' + currentDate + '</Field></Method></Batch>');

There may be something wrong with the date format you are trying to send in.

 

Cheers,

Matt

Mar 21, 2012 at 3:54 PM

Hi,

I had a similar issue while trying to update columns whith static name containing "_x0020_". You can try to update the column without "_x0020_" in the name. Thus we can check whether date format is correct and eliminate this possibility.

 

Cheers,

Kuba

 

btw. Date format should be YYYY-mm-dd

Mar 21, 2012 at 5:42 PM

@leiter84,

I've never seen anyone update a column without explicitly using the correct static name.  If "_x0020_" is a part of the static name, then it has to be put in the batch the same say.

There's some documentation on this site showing how to get the static names of your columns. (can't seem to find it right now)...

Cheers,

Matt

Mar 21, 2012 at 6:46 PM

All,

The glaring problem I'm seeing is that you are not telling SP what item to update (You need to include the <field Name="ID"> in the <method>, which tells SP what row to update in the list)... Assuming that the list that is being updated is in the same site from where this code is being executed, the following should work:

var currentDate = '2012-03-21';
var itemID      = '1';

$().SPServices({
    operation: 'UpdateListItems',
    async: false,
    listName: '{AE651F79-E723-4534-9DE1-6396BF431DEC}',
    updates: '<Batch OnError="Continue">' + 
        '<Method ID="1" Cmd="Update">' + 
            '<Field Name="ID">' + itemID + '</Field>' +
            '<Field Name="Current_x0020_Date">' + currentDate + '</Field>'    + 
        '</Method>' + 
    '</Batch>',
    completefunc: function(xData, status){
        alert("Sending the xml request to the server was: " + status);
        alert("Sharepoint processing of the message returned: " + xData.responseText);
    }
});

A few other notes:

  1. For 'listName' I normally use the visual name of the list (eg. "Action Items Register")
  2. Like Matt stated, if you want to a sub-site (or parent site), then use the 'webURL' parameter and set it to the target site (needs to be in the same domain)
  3. If having trouble finding out the internal name of a field, use SPServices $().SPServices.SPGetStaticFromDisplay() utility (See documentation

 

Paul

 

 

 

 

Mar 22, 2012 at 12:51 PM

Nice catch Paul. +10

Apr 10, 2012 at 1:09 PM

Another thing that might be causing you trouble is the date format (it appears you're trying to update a date/time field).  The date must be submitted in the ISO 8601 standard in order for SharePoint to process and store it correctly.  I had run into a similar problem.  I found a function that converts a date (or uses the current system date as a default).  When I ran a date through this conversion, everything worked fine.

function ConvertDateToISO(dtDate){
//*************************************************
//Converts Javascript dtDate to ISO 8601 standard for compatibility with SharePoint lists
//Inputs: dtDate = Javascript date format (optional)
//*************************************************
  var d;
  if (dtDate != null)  {
     //Date value supplied
     d = dtDate;
  }
  else  {
     //No date supplied, Create new date object
     d = new Date();
  }
  //Generate ISO 8601 date/time formatted string
  var s = "";
  s += d.getYear() + "-";
  s += d.getMonth() + 1 + "-";
  s += d.getDate();
  s += "T" + d.getHours() + ":";
  s += d.getMinutes() + ":";
  //Replace the "Z" below with the required
  //time zone offset (eg "+10:00" - Australian EST)
  s += d.getSeconds() + "Z";
  //Return the ISO8601 date string
  return s;
}
Apr 24, 2012 at 4:36 PM
Edited Apr 24, 2012 at 4:36 PM

awesome solution gkoliver. Thanks

Coordinator
May 31, 2012 at 12:44 AM

gkoliver:

I remembered that you had posted this function, and I just used it on a client project. Thanks!

M.

Feb 25, 2013 at 12:36 PM
Great solution from Gkoliver, just to add though I believe Sharepoint requires the time values to be double digits and when saving to a Sharepoint list I received errors when the seconds were in single digits (not sure about hours and minutes but applied the same fix to those as well just in case.

By adding this method to the Number prototype you can call it within Gkoliver's convert date function to ensure each value is double digit
Number.prototype.digitise = function (val) 
{
    return (new Array( val + 1 ).join("0") + this ).slice( - val );
}
So it would become ...
function ConvertDateToISO(dtDate)
{
//*************************************************
//Converts Javascript dtDate to ISO 8601 standard for compatibility with SharePoint lists
//Inputs: dtDate = Javascript date format (optional)
//*************************************************
  var d;
  if (dtDate != null)  {
     //Date value supplied
     d = dtDate;
  }
  else  {
     //No date supplied, Create new date object
     d = new Date();
  }
  //Generate ISO 8601 date/time formatted string
  var s = "";
  s += d.getYear() + "-";
  s += d.getMonth() + 1 + "-";
  s += d.getDate();
  s += "T" + d.getHours().digitise(2) + ":";
  s += d.getMinutes().digitise(2) + ":";
  //Replace the "Z" below with the required
  //time zone offset (eg "+10:00" - Australian EST)
  s += d.getSeconds().digitise(2) + "Z";
  //Return the ISO8601 date string
  return s;
}
Feb 25, 2013 at 1:08 PM
One more point for anyone coming across this useful function, the getYear() will not work with new browsers and has been replaced with getFullYear() so replace that in gkoliver's function or Sharepoint will throw an error when people access via new browsers.

Reviewed code:
Number.prototype.digitise = function (val) 
{
    return (new Array( val + 1 ).join("0") + this ).slice( - val );
}
function ConvertDateToISO(dtDate)
{
//*************************************************
//Converts Javascript dtDate to ISO 8601 standard for compatibility with SharePoint lists
//Inputs: dtDate = Javascript date format (optional)
//*************************************************
  var d;
  if (dtDate != null)  {
     //Date value supplied
     d = dtDate;
  }
  else  {
     //No date supplied, Create new date object
     d = new Date();
  }
  //Generate ISO 8601 date/time formatted string
  var s = "";
  s += d.getFullYear() + "-";
  s += d.getMonth() + 1 + "-";
  s += d.getDate();
  s += "T" + d.getHours().digitise(2) + ":";
  s += d.getMinutes().digitise(2) + ":";
  //Replace the "Z" below with the required
  //time zone offset (eg "+10:00" - Australian EST)
  s += d.getSeconds().digitise(2) + "Z";
  //Return the ISO8601 date string
  return s;
}
Feb 25, 2013 at 1:15 PM
hmm... iam using SPS 2010 and browser Google Chrome Version 25.0.1364.97 m, IE 10.0.9200.16484 - all works fine..
Feb 25, 2013 at 1:45 PM
I had issues with Firefox and presumed so not sure which browsers it doesn't work in

getYear() is deprecated though so definitely worth replacing
http://www.w3schools.com/jsref/jsref_obj_date.asp
Feb 25, 2013 at 1:49 PM
For some reason I cant edit my posts!!

Link is meant to be this
getYear() deprecated
Coordinator
Feb 26, 2013 at 7:10 PM
This looks like a great function to add to SPServices, unless SharePoint 2010+ has it built in. (I'm guessing it doesn't.) Kudos to gkoliver and Nostalgiaplatz.

When I take all of the comments together, I think Nostalgiaplatz's post above from Mon at 9:08 AM is the most robust.

M.
Coordinator
Feb 26, 2013 at 7:11 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Feb 26, 2013 at 10:29 PM
Great news Marc. I actually had saved off this tread to also reuse the function listed. Even better if it will be in a future version of SPServices. And there lies the value if this library as MS moves away from WebServices.

_____
Paul

Sent from mobile device.


On Feb 26, 2013, at 3:10 PM, sympmarc <notifications@codeplex.com> wrote:

From: sympmarc

This looks like a great function to add to SPServices, unless SharePoint 2010+ has it built in. (I'm guessing it doesn't.) Kudos to gkoliver and Nostalgiaplatz.

When I take all of the comments together, I think Nostalgiaplatz's post above from Mon at 9:08 AM is the most robust.

M.
Feb 27, 2013 at 12:34 PM
This is a good method. I'm a bit weary of creating an array to simply prepend a "0" to the number though. Here's what I have used a bunch:
        isoDate = function( d ) {
            //defaults to new date
            d = d || new Date();

            function pad( n ) {
                return n < 10 ? '0' + n : n;
            }

            return d.getUTCFullYear() + '-' +
                pad( d.getUTCMonth() +1 ) + '-' +
                pad( d.getUTCDate() ) + 'T' +
                pad( d.getUTCHours() ) + ':' +
                pad( d.getUTCMinutes() )+ ':' +
                pad( d.getUTCSeconds() )+ 'Z';
        },
Cheers,
Matthew