Friday, December 7, 2012

Getting List Items from a SharePoint List using SOAP

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;
     }
}


Tuesday, December 4, 2012

All (%) in Crystal Reports Formula Editor

When constructing a parameter in Crystal Reports that includes "all" items (%), construct a formula that returns true or false for your report's selection criteria.

In the formula editor, add:


//Selects all academic orgs or specific ones based on parameter
if {?Academic Orgs}="%" then  true
else
     if {?Academic Orgs} = {RDS_CLASS_TBL.CLASS_ACAD_ORG} then true
else
     false

Then for your record selection include your formula:

... and{@Selection Criteria for Academic Orgs} ...