SharePoint Applied: CAML, Your New Pet
SharePoint is a very powerful platform. It gives you a very easy-to-setup place to put your data in. And you know what happens when you have a tool like SharePoint? People use it! And then when people have been putting in data, they want to retrieve it, in all sorts of weird ways. Putting in data is only half the story, and I’d argue the easier part. It is fetching the data in a meaningful and targeted manner that separates the wheat from chaff.
Business users can be amazing. They like to hit us with scenarios we could not have imagined in our wildest dreams when we thought we had all requirements figured out. Much to my chagrin, I have heard the following, “Oh yeah, that is a new requirement!”
It is thus important that the tools developers use allow them the agility and flexibility to satisfy such needs and changing requirements.
At the very heart of it, every system is basically data in and data out. Sure, standard adages apply, good architecture, garbage in, garbage out, etc. But SharePoint builds upon the experience of many years, and thus puts in place a number of things that you, the developer otherwise would have had to worry about. For instance, every piece of content that goes into SharePoint tracks who put it there, when, who edited it last, and when they edited it. Also, a few clicks away are things such as versioning, approve content etc. But in addition to such tracking information, sometimes, you need to fetch data out of a SharePoint installation that may or may not be so straightforward.
Retrieving Data Out of SharePoint
You can choose from many ways to pull data out of SharePoint. You can use the object model and get a hold of the SPList object, and run a for/each over SPListItems. Of course, that isn’t the smartest way to filter for data though. Filtering via iteration can be extremely resource expensive.
You could use search, even programmatically. You can do so by using the FullTextSqlQuery object as demonstrated in the following article at http://blah.winsmarts.com/2008-2-SharePoint_Search_ranking_rules_and_running_it_programatically.aspx. While that will provide you with results quickly, it may or may not provide you with accurate results, and this approach certainly has an external dependency on crawl schedules and algorithms. Given the nature of search, you also have limited control over the sorting mechanisms. The sorting is controlled generally by the rank, which is dependent on an algorithm that you can influence, but not fully control.
And then you have CAML, the Collaborative Application Markup Language. SharePoint uses CAML for many purposes, one of which is extracting the very data you need, and striking an excellent balance between speed, dependability, and accuracy.
Within SharePoint you’ll find a number of objects that use CAML that can help you query for data. In this article, I will look at them one by one.
The Lists Web Service
SharePoint comes with many Web services out of the box. Web services have the innate advantage of isolating atomic pieces of functionality and thus giving you better reliability and flexibility, a concept otherwise known as SOA (service oriented architecture).
Listing 1 shows an easy way to filter out all rows modified by a given user id, using the lists.asmx Web service.
Web services have their advantages, but performance and XmlSerialization isn’t one of them. So it is reasonable to expect that you have very rich support for CAML in the object model as well. At the heart of that you’ll find the SPQuery object.
By: Sahil Malik
Sahil Malik is a Microsoft MVP, INETA speaker, a .NET author, consultant, and trainer, and a well-rounded overweight geek. He has a passion for SharePoint, data access, and application architecture.
Sahil loves interacting with fellow geeks in real time. His talks are full of humor and practical nuggets. His talks tend to get very highly charged, fast moving, and highly interactive.
You should check out his blog at http://blah.winsmarts.com