Simple WCF based REST service for SharePoint 2010, note to self

 

This is a follow-on post concerning the building of a simple WCF services in SharePoint 2010, my caveat for this style of post (notes to self) is that the information is intended truly as a “note to self” and so written that way. By now, this info is fully addressed within MSDN and several other folks around the “interwebs”. I’ll simply present the technical differences and some related bits and pieces I find useful – if you find this post useful then all the better.   For now I won’t dive into how the data would be directly returned from a class, perhaps leave that for another post.

Important: Constructing a complete WCF SOAP service is  outlined in an older post, refer to https://scottcurrier.wordpress.com/2010/07/28/simple-wcf-service-within-sp2010-note-to-self/ I’ll concentrate only on the differences for this post. The final result will be a simple WCF service consumable by JavaScript (as well as any other client that can utilize http)

 

Next, refer to http://msdn.microsoft.com/en-us/library/ff521586.aspx

 

The above link outlines the choices regarding WCF Dynamic Configuration for these style of services. The options are SOAP, REST, and ADO.NET Data Service Factories.

In this case we will use MultipleBaseAddressWebServiceHostFactory (REST)

1) So now referring back to my previous post, the first change is to switch the Service Factory to use REST instead of SOAP. Open MyService.svc and include

<% @ServiceHost Language="C#" Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory,
                                                                    Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
                                                                    Service="SampleServices.Service1, $SharePoint.Project.AssemblyFullName$" %>

 

2) Because we using the REST Factory we need to “tell” WCF how to handle the Uri by creating a Uri pattern and configure the message format (XML, Json) – the interface will look like this

using System.ServiceModel.Web

namespace SampleServices
{
   
    [ServiceContract]
    public interface IServices
    {

        // http://dev/_layouts/MyServices/MyService.svc/DoWork?name=Scott <== feel free to place the service in ISAPI or LAYOUTS
        [WebGet(UriTemplate = "/DoWork?name={name}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
        [OperationContract]
        string DoWork(string name);

     }

 

}

 

Note 1: The results can be returned as XML or JSON

see http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspx

 

Note 2: The actual Uri does not look very REST-like because of the pattern chosen in this example. This can be easily modified

See http://msdn.microsoft.com/en-us/library/bb675245.aspx

 

3) The implementation would look as follows

 

namespace SampleServices

{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class MyService : IServices
    {
        public string DoWork(string name)
        {
            return "Hello " + name;
        }

    }

}

What’s left is implementation of a more complex SP 2010 objects that I leave for the reader – but also – don’t reinvent the wheel, SP 2010 comes with plenty of ways with regard to exposing data via services that are compatible with virtually any type of client. The steps outlined in this post allow for simple but truly custom services based on your requirements.  

 

That’s it – Enjoy!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s