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(); |
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>" + |
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:
hi!!
Nice example.
Can I update "approval status" of a page in list pages ??
Thanks!
Can you please give me more detail about this ? so i can help you on that.
Thanks.
how can update Blogsite(Site)>>Posts Lists >>Approval Status and Published fields using updatelistitems method
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>" +
"<Field Name='PublishedDate'>"+ SharepointFormatDateTime(new DateTime(2009,12,12)) +"</Field></Method>";
XmlNode result = lst.UpdateListItems("Posts", batchElement);
Post a Comment