Utilizzando il web service "Lists.asmx" e in particolare il metodo UpdateListItems, abbiamo la possibilità di creare, aggiornare e cancellare elementi di liste SharePoint da applicazioni esterne. Ok lo so, questo lo sapete già...
Ma magari non sapete che se abbiamo la necessità di inserire o aggiornare dei campi di tipo data e ora, dobbiamo formattare questi valori secondo le specifiche ISO 8601. Infatti, l'utilizzo del metodo UpdateListItems in questo modo:

public static void InsertElement(string title)

{
ListsWebService.Lists service = new ListsWebService.Lists();
service.Url = "http://YOURSITEURL/_vti_bin/Lists.asmx";
service.Credentials = CredentialCache.DefaultCredentials;

XmlDocument doc = new XmlDocument();
XmlElement updates = doc.CreateElement("Batch");
updates.InnerXml = string.Format(
"<Method ID='1' Cmd='New'>" +
"<Field Name='ID'>New</Field>" +
"<Field Name='Title'><![CDATA[{0}]]></Field>" +
"<Field Name='Expires'>{1}</Field>" +
"</Method>",
title, DateTime.Now.AddDays(15));

XmlNode node = null;
node = service.UpdateListItems("LIST NAME", updates);
}

genera il seguente errore:

ErrorCode: 0x8102001c
ErrorText: Invalid date/time value. A date/time field contains invalid data. Please check the value and try again.

Questo perchè la data non è stata formattata correttamente secondo il formato ISO 8601. Per farlo è possibile richiamare il metodo ToString() della classe DateTime passando come parametro la formattazione "yyyy-MM-ddThh:mm:ssZ".

DateTime.Now.AddDays(15).ToString("yyyy-MM-ddThh:mm:ssZ");


Oppure, onde evitare possibili errori di scrittura della stringa di formattazione della data, possiamo utilizzare il metodo CreateISO8601DateTimeFromSystemDateTime della classe SPUtility.

public static void InsertElement(string title)

{
ListsWebService.Lists service = new ListsWebService.Lists();
service.Url = "http://YOURSITEURL/_vti_bin/Lists.asmx";
service.Credentials = CredentialCache.DefaultCredentials;

XmlDocument doc = new XmlDocument();
XmlElement updates = doc.CreateElement("Batch");
updates.InnerXml = string.Format(
"<Method ID='1' Cmd='New'>" +
"<Field Name='ID'>New</Field>" +
"<Field Name='Title'><![CDATA[{0}]]></Field>" +
"<Field Name='Expires'>{1}</Field>" +
"</Method>",
title, SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now.AddDays(15)));

XmlNode node = null;
node = service.UpdateListItems("LIST NAME", updates);
}


Davvero utile !


TAGS: [SharePoint] [WSS 3.0] [MOSS 2007] [SPUtility]
Commenti(0) - Posted @ 3/3/2010 11:17:30 AM - Categoria: Sharepoint - Permalink - Share on twitter | facebook


COMMENTI
Non sono stati aggiungi commenti ...

INSERISCI UN COMMENTO

Nome *
Indirizzo e-mail
(non verrà pubblicato)
Commento *