Sunday, January 18, 2009

Update DateTime Column Using SharePoint ListService

SharePoint webservice can be used to update item or items in SharePoint List.When SharePoint List contains “Date And Time” Column with only Date or Date And Time Mode, you have to pass date or datetime in specific format so it can be update by webservice.

UpdateListItems method is used for this purpose.

To format DateTime for SharePoint Webservice, i created following extension methods.

// Extensions Method

public static class DateTimeExtensions
{
    // Only Date Column
    public static string SharepointFormatDate(this DateTime dt)
    {
        return dt.ToString("yyyy-MM-dd");
    }

    // Date And Time column
    public static string SharepointFormatDateTime(this DateTime dt)
    {
        return dt.ToString("yyyy-MM-ddTHH:mm:ssZ");
    }
}

To Update List using webservice.

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();           
System.Xml.XmlElement batchElement = doc.CreateElement("Batch");
batchElement.SetAttribute("OnError", "Continue");
batchElement.SetAttribute("ListVersion", "1");
batchElement.InnerXml =”<Method ID='1' Cmd='Update'>" + "<Field Name='ID'>2</Field>" +
"<Field Name='OrderDate'>”+ DateTime.Now.SharepointFormatDate() +”</Field><Field Name='OrderDateTime'>”+ DateTime.Now.SharepointFormatDateTime()  +”</Field></Method>";

// List Serivce object  (Add web reference for this)
Lists lst = new Lists();
lst.Url = “http://<your site>/_vti_bin/lists.asmx” ;
lst.Credentials = new System.Net.NetworkCredential("test", "test");
lst.UpdateListItems(“Orders”,batchElement);

In above code <Field Name=’ID’>2</Field> is used to identify unique row that need to be update. In Sample only item with id 2 is updated. In order to update multiple items just add another method element. For example, (This update both item with id 2 and 3)

batchElement.InnetXml =

“<Method ID='1' Cmd='Update'>" + "<Field Name='ID'>2</Field>" +
"<Field Name='OrderDate'>”+ DateTime.Now.SharepointFormatDate() +”</Field><Field Name='OrderDateTime'>”+ DateTime.Now.SharepointFormatDateTime()  +”</Field></Method>" +
"<Method ID='2' Cmd='Update'>" + "<Field Name='ID'>3</Field>" +
"<Field Name='OrderDate'>”+ DateTime.Now.SharepointFormatDate() +”</Field><Field Name='OrderDateTime'>”+ DateTime.Now.SharepointFormatDateTime()  +”</Field></Method>"

Same way you can update another type of columns too.

More information available at following location.

http://msdn.microsoft.com/en-us/library/lists.lists.updatelistitems.aspx
http://msdn.microsoft.com/en-us/library/ms440289.aspx

4 comments:

Anonymous said...

hi!!

Nice example.

Can I update "approval status" of a page in list pages ??

Thanks!

dotnetstep said...

Can you please give me more detail about this ? so i can help you on that.

Thanks.

Janardhan said...

how can update Blogsite(Site)>>Posts Lists >>Approval Status and Published fields using updatelistitems method

dotnetstep said...

You can use this way to update published column whoes actual name PublishedDate should be used to update via web service.

Lists lst = new Lists();
lst.Url = "http://avani2003/jblog/_vti_bin/lists.asmx";
lst.Credentials = new System.Net.NetworkCredential("username", "password","domain");

XmlDocument doc = new XmlDocument();
XmlElement batchElement = doc.CreateElement("Batch");
batchElement.SetAttribute("OnError", "Continue");


batchElement.InnerXml = "<Method ID='1' Cmd='Update'>" +
"<Field Name='ID'>1</Field>" +
"&ltField Name='PublishedDate'>"+ SharepointFormatDateTime(new DateTime(2009,12,12)) +"</Field></Method>";

XmlNode result = lst.UpdateListItems("Posts", batchElement);