Use the steps below to get list items from a SharePoint list using SOAP.
First construct your xml SOAP message:
StringBuilder sbEnvelope = new StringBuilder();
sbEnvelope.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sbEnvelope.Append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
sbEnvelope.Append("<soap:Body><GetListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">
<listName>" + listname + "</listName><viewName>" + viewName + "</viewName>" +
"<query></query><viewFields><ViewFields><FieldRef Name='Column1' /><FieldRef Name='Column2' /><FieldRef Name='Column3' /></ViewFields></viewFields></GetListItems></soap:Body></soap:Envelope>");
Next, construct your HttpWebRequest, get your response and place it in an XmlDocument for parsing.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://<site>/_vti_bin/lists.asmx");
req.Method = "POST";
req.ContentType = "text/xml; charset=\"utf-8\"";
req.Accept = "text/xml";
req.Headers.Add("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems");
req.UseDefaultCredentials = true;
using (Stream stream = req.GetRequestStream())
{
using (StreamWriter writer1 = new StreamWriter(stream))
{
writer1.Write(sbEnvelope.ToString());
}
}
WebResponse response = req.GetResponse();
Stream responseStream = response.GetResponseStream();
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(responseStream);
Finally, create your namespaces and parse the xml.
XmlNamespaceManager manager = new XmlNamespaceManager(xmlDocument.NameTable);
manager.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/");
manager.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
manager.AddNamespace("z", "#RowsetSchema");
manager.AddNamespace("y", "http://schemas.microsoft.com/sharepoint/soap/ois");
manager.AddNamespace("w", "http://schemas.microsoft.com/WebPart/v2");
manager.AddNamespace("d", "http://schemas.microsoft.com/sharepoint/soap/directory");
XmlNodeList NodeList = xmlDocument.SelectNodes("//sp:listitems/rs:data", manager);
String t = "";
foreach (XmlNode ListItem in NodeList)
{
foreach (XmlNode node in ListItem.ChildNodes)
{
t = node.Attributes["ows_Title"].Value;
}
}
No comments:
Post a Comment