In some of my previous posts I discussed a “beefy” project called XAPTIC which is a pure Silverlight web part framework. It’s a complex enough project to keep me interested and learning. Originally conceived as Silverlight “living” within an asp.net web application. This year I decided to move it into SharePoint 2010 as a full fledged environment with high end capability to construct SharePoint applications easily. It also makes great “source material” for my blog posts as well.
The roadmap for migrating XAPTIC to SharePoint 2010 includes creating a XAPTIC service application and exposing its functionality via custom SharePoint REST based services. I can report that the service application is nearly done and deploys properly to SharePoint but that will be the subject for later posts.
For this post I’ll discuss using REST to POST objects (including potentially complex objects) to SharePoint 2010. The information presented here is somewhat preliminary and not “production” ready as yet but serves me well during this phase of development and my understanding.
Assuming you’re sending data back to SharePoint as a REST style service you would need to be concerned with several things – remember, unlike SOAP, you have no proxy – its all hand rolled (at least using the basic OTB classes). Here are some considerations
- How to represent your class and instance data
- Maintaining a well formed request (fiddler helps here)
- Code to properly create that REST style request
- For Silverlight, you need to keep everything asynchronous
For XAPTIC, I know the classes and parameters I need and therefore I can make highly specific decisions that keep my code rather simple.
For a little more background on building REST based services in SharePoint 2010 see the following links
That being said, I’ll cut to the bottom line. In my experimenting I found the best way to represent your class is by simply convert to an XElement. See
I created a simple “converter” method to do just that (see code) ConvertUserFieldsToXElement(object) and used it as part of a method that POSTs back to SharePoint 2010. I won’t go into the details of the Converter method because its basic. More likely, if you’re understanding this post you would have little trouble designing one for a class of your own.
The following is the entire method used to POST back the data
You see I also pass two parameters called internalID and pageTitle as well. By the way, hard coding the URL is not recommended but good enough for a blog post. I felt converting my objects to XElement was by far the most flexible method of class representation and very easy to understand. I originally just serialized my object as text but had some difficulty with white space and related artifacts of the serialization in the body of the request message (but that’s your choice – it all works the same).
Here is the actual fiddler trace
This method is taken directly from XAPTIC’s Silverlight client – it’s job is to pass back data to SharePoint’s XAPTIC database and update parameters from a users web part placed within the canvas. It does this by first using the HttpWebRequest class forming a proper HTTP request but then adds fields (a LIST<> of field) to the body of the request. The internalID and pagetitle parameters are passed only as part of the URL. Note that I am using anonymous methods for both the request and response handling – that piece really simplifies the code.
The interface on SharePoint looks like this:
Of course, this is early code. I’m sure feedback could include comments about using WebClient and possibly more contemporary tools to accomplish the same goal – let me know
That’s it – it works well and brings me a step closer to XAPTIC in SharePoint 2010
Thanks – Scott