Using LINQ to XML to query SharePoint lists from WPF

A colleague of mine (a distinguished architect at Microsoft-you know who you are) convinced me to try LINQ for working with data from SQL recently after seeing my old fashioned SQL Command code in an implementation of SPEventReceiver I had developed.  Slightly embarrassed at the thought I had copied this code from a project I did going on three years ago and thinking LINQ to XML actually came in handy on a couple Silverlight projects where I was pulling data from web services I finally succumbed to peer pressure and dug more into this when recently having a need to connect to SharePoint web services from a WPF client.

It was relatively painless to use LINQ for parsing the data returned from the lists.asmx web service (especially compared to the XMLDOM) and the thought occurred to me that XDocument would also work with the trusty owssrv.dll ISAPI extension in WSS.  The task: A Silverlight project I am working on for a client where on boarding an ASPNET web application is a long and tedius process to get through IT but deploying a Silverlight 2 application is just to drop the test page and .XAP file.  Wanting to avoid resorting to Silverlight V1 (not that I don’t like javascript but prefer C# when building out the SL UI dynamically and attaching animations) I broke out my trusted Swiss army knife for SharePoint  owssrv.dll and the URL protocol usually reserved for accessing list data via javascript: check out the URL Protocol on MSDN

Whaa-laa, a few lines of code and I was actually able to modify the existing code that was previously using getlistitemsfrom the SharePoint built-in lists.asmx web service to use owssrv.dll instead from WPF and preserve all the databinding in my WPF controls-very cool.

How it works:
Use LINQ to XML to load the list data as XML into a LINQ XDocument and build a simple XML tree (you can use this to bind to a WPF or Silverlight control using a data item template or populate a XAML container from code.  The cool thing is using this method you won’t need to add a web reference to your project and for Silverlight applications this can simplify deployment since you won’t have to create a corresponding ASPNET web application to host your Silverlight application as you would if using the web service.

Code Sample:

//specific references to add for this sample

using System.Linq
using System.Xml;
using System.Xml.Linq;

XNamespace z = “#RowsetSchema”;

string sUrl = http://[server]/[site]/_vti_bin/owssvr.dll?Cmd=Display&List={[listguid]}&XMLDATA=TRUE;

//View ID “{[viewid]}” //opitonal parameter to owssrv.dll

//this part resolved a security exception I was getting while accessing from the
//localhost

XmlUrlResolver xmlResolver = new XmlUrlResolver();

xmlResolver.Credentials = System.Net.CredentialCache.DefaultCredentials; XmlReaderSettings xmlReaderSettings = new XmlReaderSettings(); xmlReaderSettings.XmlResolver = xmlResolver;

XDocument xdoc = XDocument.Load(XmlReader.Create(sUrl, xmlReaderSettings));

XElement notifications = new XElement(“Notifications”,

xdoc.Root

.Descendants(z + “row”)

.Select(r => new XElement(“row”,

r.Attribute(“ows_ActorName”), //Use whatever fields are returned from owssrv
r.Attribute(“ows_ActorID”),      //for your particular list.
r.Attribute(“ows_Action”),       //Tip: just copy the value of sUrl into the browser
r.Attribute(“ows_ItemTitle”),   //to view the returned fields and XML from your
r.Attribute(“ows_ItemType”),  //particular list
r.Attribute(“ows_ItemURL”),
r.Attribute(“ows_ListTitle”),
r.Attribute(“ows_ListUrl”),
r.Attribute(“ows_EventTime”),
r.Attribute(“ows_ListType”)

)

)

);

//Use whatever fields are returned from owssrv
//for your particular list.
//Tip: just copy the value of sUrl into the browser
//to view the returned fields and XML from your
//particular list

What’s Next? Trying this out with an RSS feed tonight maybe.

Summary: LINQ to XML definitely simplifies parsing and working with the data returned from the SharePoint web services and also appears to work just as easy with the WSS Url Protocol and am sure with RSS feeds as well.  I haven’t bench-tested performance and scalability for owssrv.dll in this particular application so please keep that in mind-plus one known bug (or feature::) of owssrv.dll is that it doesn’t appear to be accessible if your SharePoint is running under anonymous access.

Coming Soon:Using the SharePoint Lists.asmx web service and handy LINQ to XML from Silverlight 2 for populating your Silverlight controls with data from SharePoint.

-Rod