Tuesday, 24 November 2009

Create Custom Service Application in SharePoint 2010 – Part 1

Create Visual Studio Solution

In Visual Studio 2010, create a new project using the “Empty SharePoint Project” and call it “HelloServiceApplication”.

You will be asked for the SharePoint site address for debugging. Since we are deploying this to the central administration site, enter the URL of Central Administration site and check the “Deploy as a farm solution” radio button. Click “Finish” to continue.

We will change the assembly name and default namespace to “SharePointEgg”. Right click the “HelloServiceApplication” and select “Properties”. Under the “Application” tab, change both the “Assembly name” and “Default namespace” to “SharePointEgg”. Save the settings.

Add a reference to the following assembly:

  • System.ServiceModel
  • Microsoft.SharePoint
  • Microsoft.Office.Server
  • Microsoft.Office.Server.UI
  • System.Web.DataVisualization
Create WCF Contract and Service Host Declaration

We start this by adding a mapped folder to the WebServices folder in {SharePointRoot}. To do this, right click the HelloServiceApplication > Add > SharePoint Mapped Folder..., then select the “WebServices” folder and click “OK”. A new WebService folder “HelloServiceApplication” will be added to the project.

Let’s add a contract file that has a single signature “Hello”. Right click the WebServices/HelloServiceApplication folder and add a new item. Select the “WCF Service” project template and give it a name called HelloService.

Open IHelloService.cs and declare a signature “Hello”.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace SharePointEgg.WebServices.HelloServiceApplication
{
    [ServiceContract]
    public interface IHelloService
    {
        [OperationContract]
        string Hello(string name);
    }
}

Next we will add a service host declaration. Add a text file type in the WebServices\HelloServiceApplication folder and rename it to “Service.svc”. Open the svc file and enter the following declarative:

<%@ServiceHost language="C#" Debug="true" Service="SharePointEgg.HelloServiceApplication, SharePointEgg, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c6000eb8b348f36b" %>

Note that you need to enter your own public key token.

We will add a configuration file to our WCF service that contains the information such as the behaviour. Right click WebServices\HelloServiceApplication folder again and this time we will add an application configuration file called “web.config”. Enter the following xml to the newly added file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="TestBehaviour" name="Hello Service Application">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="" contract="SharePointEgg.WebServices.HelloServiceApplication.IHelloService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="TestBehaviour">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.webServer>
    <security>
      <authentication>
        <anonymousAuthentication enabled="true" />
        <windowsAuthentication enabled="false" />
      </authentication>
    </security>
  </system.webServer>
</configuration>

Delete HelloService.cs as we don’t need it. Also delete the app.config added to the base project.

We have successfully completed a simple WCF service that is ready to deploy to a SharePoint farm.

No comments: