Creating WCF service in SharePoint 2010

Whether you already tried creating WCF service as a standalone website project or WCF library – creating SharePoint hosted WCF add whole new meaning to it :)

Suppose you want to create a service which you can call using URL and receive and XML or JSON back in your browser or JavaScript app.

I’ll demonstrate the process step by step:

In our case we’ll call 

http://localhost/_vti_bin/Service1.svc/MyCall?message=Some Text

  and receive

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
You entered: Some Text, Entered on: 8/31/2010 7:19:29 PM</string>

1. Create new Visual Studio 2010 Blank SharePoint project.

2. Map a new SharePoint folder in your solution – it’ll be ISAPI folder. That’s where our Service definition is going to sit.

3. Add a new Text file in the newly mapped ISAPI folder. Rename the text file to SVC extension and add the following code to it:

<%
@ServiceHost Language="C#" Debug="true" Service="WCFOnSP.Service1, 
WCFOnSP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=45738fb2ebc34cfd
Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory,
Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" %>

Ensure you replace highlighted parts with valid assembly information and namespaces.

4. Add the following references to your project:

Microsoft.SharePoint.Client.ServerRuntime.DLL – (C:\Windows\assembly\GAC_MSIL\Microsoft.SharePoint.Client.ServerRuntime\ 14.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.ServerRuntime.dll)

System.ServiceModel.DLL

System.ServiceModel.Web.DLL

5. Add a new folder to your solution called WCF Service (or anything else). This is where we`re going to place our service classes and contract.

6. Add new class called IService1.cs - which is the service contract. Replace it`s code with the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
namespace WCFOnSP
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet(UriTemplate = "/MyCall?message={message}",
            BodyStyle = WebMessageBodyStyle.Bare,
            ResponseFormat = WebMessageFormat.Xml)]
        string GetData(string message);
        // TO Call this method - deploy the solution and call the following URL:
        // http://localhost/_vti_bin/Service1.svc/MyCall?message=Some Text
    }
}

Pretty self explanatory – I event included a comment on what`s the call syntax. WebMessageFormat.Xml will determine if the return is XML or JSON – so this is how you toggle it.

7. Now let`s implement the REST call method. Create new class in the same folder as your contract – name the file: Service1.cs, replace it`s content with the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using Microsoft.SharePoint.Client.Services;
namespace WCFOnSP
{

    [BasicHttpBindingServiceMetadataExchangeEndpointAttribute]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class Service1 : IService1
    {
        public string GetData(string message)
        {
            return string.Format("You entered: {0}, Entered on: {1}", message,  DateTime.Now.ToString());
        }
    }
}

Also, pretty self explanatory – this method can really execute anything as long as it returns a string value. Attributes on a top of the class will ensure the service has necessary resources to run within the environment – which you normally get by default in SP 2010.

That`s all there is to it – deploy your solution and use the calling URL from above – you should get your desired message in XML. Now you can consume the XML or JSON in your application.

Good Luck!

This entry was posted in sharepoint, sharepoint 2010 and tagged , , . Bookmark the permalink.

Comments are closed.