One of my recent projects involved creating a custom web part for SharePoint Online (part of the Office 365 suite). Unfortunately, one of the limitations of developing for the cloud is that you’re bound to a sandboxed solution environment. This particular project required retrieving data from the User Profile Service using the following SharePoint web service:
1.) Query Web Service
Now, if I were not limited to a sandboxed environment my first approach would be to use the SharePoint object model. In the sandbox, however, we are limited to the Microsoft.SharePoint.dll and any assemblies which include the attribute: “AllowPartiallyTrustedCallersAttribute” as outlined here:
My solution….. use the SPServices library to make the web service calls from the client side.
SPServices is a jQuery library that wraps SharePoint’s Web Services calls using the .ajax() method behind the scenes, I would recommend to anyone reading this blog post to check out their web site for more information. I found the library very easy to use and their site is full of good documentation.
Before using the SPServices library, you’ll have to download the jquery.SPServices-0.6.1.js and save it to an accessible location (for this example I added “jquery.SPServices-0.6.1.js” to the SharePoint Site Assets folder on my site) and use the following reference:
The best thing about SPServices is that it does all of the heavy lifting for you, saving you the time of coding the Ajax calls and constructing the SOAP envelope.
For an outline of the ‘general syntax’ of the SPServices function I used, refer to the following link:
Lets take a look at how I used the SPServices function:
Every Web Service call will follow a similar structure with different parameters according to the operation. For example, if you look at line 2 from the snippet above, the ‘operation’ parameter is set to “Query” which indicates to the library that it will be calling the Query Web Service. On line 5, ‘completefunc’ accepts a reference to the callback function that should be executed upon completion of the Web Service call. For this example, I’m setting the response XML to a variable, ‘xmlProfileResponse’ and then parsing it later. Finally, the ‘queryXml’ parameter on line 4 is where the search criteria is defined. Here is the query itself:
As you can see, it’s a straight forward query. In line 11 above I’m filtering by lastName. Notice that each of the values in the Select clause (line 9) come from the User Profile Service’s default fields. The scope is defined by line 10, SPSPeople.