Calling a SharePoint list using Web Service using GetListItems now expects XElement
I recently ran into a problem trying to run CAML queries against a SharePoint 2010 list using the web service and the stub code created by Visual Studio 2010. It seems that Microsoft has made some changes so the GetListItems method now expects the query parameter to be of type System.Xml.Linq.XElement instead of an XMLNode.
This article in SharePoint Magazine covers the CAML queries but I found that examples given produced an error
"Cannot convert System.Xml.XmlNode to System.Xml.Linq.XElement"
In this example I was trying to query a list for all list items that did not have a “Status” of “Completed”. Here is the code that finally worked.
[code language=”csharp”]
var spquery = new System.Xml.Linq.XElement(“Query”,
new XElement(“Where”,
new XElement(“Neq”,
new XElement(“FieldRef”,
new XAttribute(“Name”,”Status”)),
new XElement(“Value”
new XAttribute(“Type”,”Choice”),”Completed”))));
var viewFields = new System.Xml.Linq.XElement(“ViewFields”);
var queryOptions = new System.Xml.Linq.XElement(“QueryOptions”);
XElement items = ws.GetListItems(“Example List”,
“{D4200E79-28AF-4DF0-A106-499226DD38DB}”,
spquery,
viewFields,
null,
queryOptions,
null);
[/code]
This produces the following XML for the query…
[code language=”xml”]
[xml light=”true”]
<Query>
<Where>
<Neq>
<FieldRef Name=”Status'” />
<Value Type=”Choice”>Completed</Value>
</Neq>
</Where>
</Query>
[/code]