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

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”,


.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




//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.



10 thoughts on “Using LINQ to XML to query SharePoint lists from WPF

  1. Hmmm, this could be very interesting, will it run on IW?-p

  2. Thank you! This is going to be very helpful to me. Would you happen to have any examples of populating a silverlight treeview with the data returned from a SharePoint lists service. I\’m currently building a treeview to navigate a PhotoGallery.

  3. How do you deal with folders? I\’m only getting the top level folders when I use this.

  4. Yes, will run on IW. In fact IW was my inspiration:)

  5. Jamie-I will post the example of using the Lists.asmx web service shortly-also a Silverlight example of binding the data to a control using a simple data item template.

  6. Thanks! Very much appreciated. I have searched high and low on how to deal with folders and subfolders while using owssvr but I have had no luck. I\’m building a photo gallery with a SharePoint Picture Library being the source. Each folder in the library is an album. Thanks again!

  7. No Name-you should check out Darren Johnstone\’s javascript library for accessing SharePoint web services from client-side code-particularly the example for accessing the lists.asmx web service from his handy javascript library since the web service is a better option for recursive operations-getlistitems for your particular picture library for instance.

  8. Ok runs on Silverlight but use WebClient to connect to owssrv.dll instead of XDocument i.e. WebClient sp = new WebClient(); sp.OpenReadCompleted += new OpenReadCompletedEventHandler(sp_OpenReadCompleted); sp.OpenReadAsync(new Uri(sUrl));Also, for using owssrv.dll and/or lists.asmx you can drop the clientaccesspolicy.xml file at the root of your SharePoint via SP Designer, and remember to test your data-access while running in the context of SharePoint or you will get an error believe me. For simplicity I just drop the test page and .XAP file via SP Designer.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s