SharePoint - here is a REST service Project Item template
UPDATE:
There seems to be an issue when loading this template in VS.NET - sometimes there's an error that prevents the rest of CKSDEV from working correctly. If you are unable to retract or deploy via CKSDEV after this template was included, please remove this template and restart VS.NET
CKSDEV provides a wonderful WCF template. The main magic of this template is that it uses SharePoint 2010's MultipleBaseAddressBasicHttpBindingServiceHostFactory to create a service host and hook up all the configuration settings. This is done by the hardwork Charlie Holland and described here http://www.chaholl.com/archive/2010/03/10/how-deploy-a-wcf-service-to-sharepoint-2010.aspx
I've been presenting a talk where you can start with the WCF template, then change the factory to MultipleBaseAddressWebServiceHostFactory and switch the service to a REST service.
Finally, I sat down and created a Project Item template for SharePoint. With examples on how to create both a GET and a POST method in REST service.
Download:
Install:
- You may need to unblock this file if you download it from IE
- Copy the file to \Documents\Visual Studio 2010\Templates\ItemTemplates\Rest.zip
- Close / Restart VS.NET
Use:
- Create a blank SharePoint Farm solution. Services can't be deployed as Sandbox.
Figure: Create a SharePoint Farm solution. - Add new Project Item, the template does NOT appear under the SharePoint group currently. So you'll need to search for it. Will be fixed... "Soon". Give it a nice name.
Figure: Search for the template and use it to create a REST service - These files are created:
Figure: My RestService1
- The IRestService1.cs interface gives you the definition of your REST service methods. I've provided a GET and a POST operation.
Figure: GET, and POST - You should also add a CKS WCF Service
- The solution should look like this:
Figure: OK, we have a REST service and a WCF SOAP service.
Important. CKSDEV's WCF Template hooks up token replacement for the *.svc file. You need to add this, even if you don't use it, otherwise you'll need to modify your VS.NET project file manually. You can remove this service after you've added it once.
I recommend you compare the difference between the CKSDEV WCF SOAP service and the REST Service. - Deploy!
Figure: I LOVE Deploy from VS.NET 2010 - Open a browser, go to:
http://server/_vti_bin/SharePointProject1/RestService1.svc/GetItem/1
You should get:
- The returned content type is unrecognized by Internet Explorer, so it's prompting you to save it.
Save this, and open it in notepad.
Figure: Haha! JSON'ed HelloWorld Blob object returned from the server!
- In IE Developer Toolbar, you can watch the network traffic
This particular request took 31ms and sent 463 bytes!
Figure: Extremely fast and efficient way to talk to SharePoint.
If you see this error:
ErrorThe type 'SharePointProject1.RestService1, $SharePoint.Project.AssemblyFullName$', provided as the Service attribute value in the ServiceHost directive could not be found.
This is because VS.NET does not perform Token Replacement for the *.svc file by default. There are a few ways to fix this.
- The simplest way is to add a CKSDEV WCF Service Project Item. You can remove it afterwards.
- Alternatively, you can edit your project file, you need to find this, and add svc;
Reload your project
Minor issues need fix... one day:
Minor Problems:
- Project Item doesn't appear under SharePoint Group
To properly create SharePoint Project Items, I need to define a proper .spdata file - this requires an additional project to declare a new SharePoint Project Item type.
This will also allow me to specify an ICON to use in the solution, instead of this silly red looking icon. - Not properly set up TokenReplacementFileExtension
The project will also allow me to hook into the project item wizard and link up TokenReplacementFileExtension for svc. - Need custom code...
Custom code requires signed assemblies. Annoying. - Or, preferably, I clean up the template a bit more and submit it to CKSDEV, which has most of these facilities already built in.