For the latest thing I am doing at work I need to be able to grab some data from Sharepoint. I have chosen to do this by consuming SPServices.asmx server that is available. Because I was not able to find a decent post on the net on how to use/consume this service, I thought I would create my own. For this post I am going to walk though using the QueryDocuments method. As I use/consume more methods, I will try to post on those as well.
Before you get started working with the Sharepoint Web Services, I would recommend you download the SDK from here. I would then suggest you install it and poke around the CHM file.
Service Location
FYI, if you download the SDK, this will NOT install any assemblies on your drive (see my rant about this here).
You will be able to find the ASMX files on the box with Sharepoint install in the following location.
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI
You will be able to find the ASMX interface via a browser here
http://servername/_vti_bin/SPServices.asmx
All the web services can also be found under each and every sub-site that is created.
Example
http://servername/subsitename/_vti_bin/SPServices.asmx
Consuming the service
The purpose of this post is to not teach you how to consume the post, however you will only need to open up VS2005 and do an add Web Reference and you will be all set
CAML
The different Sharepoint Web services make have use of CAML (Collaborative Application Markup Language). CAML is an XML-based language that is used in Microsoft Windows SharePoint Services to define the fields and views used sites and lists. CAML is also used to define tables in the Windows SharePoint Services database during site provisioning.
Resources on CAML
- SDK - Within the SDK, there are is a good section on CAML. Go to the index tab and search on CAML.
- How to use CAML here
- CAML query builder tool here
Writing code to consume the service
Once you have consumed the service, it is time to start coding against it.
In order to successfully call the QueryDocuments method you will need to know a few things.
- What site/sub-site you are trying to query data from
- What library (assuming here document library)
- What field you are using in your query.
Once you know the following, you are pretty much set. Below is sample code that should help you connect and query data. (BTW, this is NOT production code, but simple a dumping into one method for sake of this post)
Here are a few lines explained
EQUALS_QUERY_2PARAMS - This is the CAML query used to return data from the web service call. This can be changed to fit your needs, see the CAML specs for more information about how to do this.
service.Credentails - This needs to be set to DefaultCredentials in order to work.
service.URL - Because the way that SharePoint stores data, you will need to set the URL at run time. Sharepoint will only allow you to access the data from the given sub-site that your service is in.
service.QueryDocuments - This is the call into the web service to get the actual data.
foreach statement - When the service call returns, it returns a populated dataset object. In order to get data out you will need to loop through and get each row. There may be better ways, but this is simple and works.
There you have it, a simple straight forward way to use the SPServices.asmx service from Sharepoint.
Till next time,