SharePoint Webservices - oder wie ich CAML lieben lernte....

Die Verwendung der SharePoint Webservices (hier speziell dem Lists.asmx) ist hinreichend beschrieben (z.B. hier)

Bei der Verwendung sind mir jedoch zwei Dinge aufgefallen die mir beim Entwickeln immer Zeit (und manchmal auch Nerven) kosten:

  • Wie sind die Feld/Spaltennamen die ich als FieldRef eintragen muss ?
  • Wie sieht die Filterbedingung in CAML aus die ich verwenden muss ?

Lösung: CAMLQueryBuilder

Es gibt jedoch in den einschlägigen SharePoint Tools von U2U den CAMLQueryBuilder der genau diese Probleme adressiert. Das Schöne an dem Tool ist dass man die Queries direkt auf Demo/Testdaten bauen kann und die Ergebnisse auch gleich auf Plausibilität überprüfbar sind.

Wenn man diese Hürde genommen hat lassen sich Abfragen gegen den Lists.asmx zum Ermitteln der Listeneinträge recht komfortabel wie folgt erstellen:

 // Declare and initialize a variable for the Lists Web Service.
server.Lists listService = new server.Lists();
/* Authenticate the current user by passing their default
credentials to the Web Service from the system credential cache.*/
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Set the Url property of the service for the path to a subsite.
listService.Url = "https://server/_vti_bin/Lists.asmx";
// Instantiate an XmlDocument object
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
/* Assign values to the string parameters of the GetListItems method,
using GUIDs for the listName and viewName variables*/
string listName = "Contacts";
string rowLimit = "150";

/*Use the CreateElement method of the document object to create elements
for the parameters that use XML*/
System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");

/*To specify values for the parameter elements (optional), assign CAML fragments
to the InnerXml property of each element*/
query.InnerXml = "<Where><Gt><FieldRef Name=\"ID\" />" + "<Value Type=\"Counter\">3</Value></Gt></Where>";
viewFields.InnerXml = "<FieldRef Name=\"Title\" />";
            
viewFields.InnerXml="<FieldRef Name='ID'/><FieldRef Name='Title'/>" +
                    "<FieldRef Name='FirstName'/><FieldRef Name='FullName'/>" +                                    
                    "<FieldRef Name='Email'/><FieldRef Name='Company'/>" +
                    "<FieldRef Name='JobTitle'/><FieldRef Name='WorkPhone'/>" +
                    "<FieldRef Name='HomePhone'/><FieldRef Name='CellPhone'/>" +
                    "<FieldRef Name='WorkFax'/><FieldRef Name='WorkAddress'/>" +
                    "<FieldRef Name='WorkCity'/><FieldRef Name='WorkState'/>" +
                    "<FieldRef Name='WorkZip'/><FieldRef Name='WorkCountry'/>";
                                    
            
    queryOptions.InnerXml = "";

/* Declare an XmlNode object and initialize it with the XML response from
the GetListItems method.*/
System.Xml.XmlNode nodeListItems = listService.GetListItems(listName, "",query,viewFields,rowLimit,queryOptions);
// Loop through each node in the XML response and display each item.
System.Data.DataSet  myds = new DataSet("myds");
System.IO.StringReader xmlSR = new StringReader(nodeListItems.InnerXml);
myds.ReadXml (xmlSR);
//dataGrid1.DataSource=myds.Tables[1];

Viel Spass beim ausprobieren

Sven