I Got a requirement to access a list data through web service. Therefore, I consumed 'Lists.asmx' built-in service provided by SharePoint.
1) Following codes are required to get list item through CAML query
Lists listService = new Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
listService.Url = "http://spsserver" + "/_vti_bin/lists.asmx";
XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
XmlNode ndViewFields =
xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
XmlNode ndQueryOptions =
xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
ndViewFields.InnerXml = "";
ndQuery.InnerXml = "" +
"0";
try
{
XmlNode ndListItems = listService.GetListItems("Tasks", null, ndQuery, ndViewFields,null, null, null);
foreach (System.Xml.XmlNode listItem in ndListItems.ChildNodes[1].ChildNodes)
{
if (listItem.OuterXml.Contains("ows_Title"))
{
txtValue.Text += Environment.NewLine + listItem.SelectSingleNode("@ows_Title").Value.Trim();
string strStartDate= listItem.SelectSingleNode("@ows_StartDate").Value.Trim();
DateTime dtConvert = DateTime.SpecifyKind(DateTime.Parse(strStartDate), DateTimeKind.Utc);
txtValue.Text += " " + dtConvert.ToLocalTime().ToString();
}
}
}
catch (System.Web.Services.Protocols.SoapException ex)
{
MessageBox.Show("Message:\n" + ex.Message + "\nDetail:\n" +
ex.Detail.InnerText +"\nStackTrace:\n" + ex.StackTrace);
}
2) This code is for updating a list item through batch update
try
{
string strBatch = " 2" +
"a test data";
XmlDocument xmlDoc1 = new System.Xml.XmlDocument();
System.Xml.XmlElement elBatch = xmlDoc1.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
elBatch.SetAttribute("ListVersion", "1");
elBatch.InnerXml = strBatch;
XmlNode ndReturn = listService.UpdateListItems("Tasks", elBatch);
MessageBox.Show(ndReturn.OuterXml);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}