Tuesday, December 24, 2013

Creating and consuming web service in Share Point Hosted environment

The service-oriented applications can be build using WCF ( windows communication foundation). Using WCF, data can be sent in asynchronous way from one service endpoint to another.
The WCF is mostly used in following scenarios.
A Silverlight application to poll a service for the latest data feeds
A chat service that allows two people to communicate or exchange data in real time.
A secure service to process business transactions.
A service that supplies current data to others, such as a traffic report or other monitoring service.
A dashboard application that polls one or more services for data and presents it in a logical presentation.
Exposing a workflow implemented using Windows Workflow Foundation as a WCF service.

The WCF can be created as one of the feature and can be deployed to hosted share point environment. It has to deploy to ISAPI folder as WSP package or under web application vti_bin folder. Once it’s added to share point then it can used in any web application, Silver light application or any other application as interface to share point.
Below some description provided for creating share point hosted WCF and consuming it in your application.
To prepare this article I have taken the help from MSDN.
To create a SharePoint Foundation project for the WCF service, open the ProjectTracker solution in Visual Studio. In Solution Explorer, click the solution. On the File menu, point to Add, and then click New Project. In the Installed Templates tab of the Add New Project dialog box, expand Visual C# node, select SharePoint, select Empty SharePoint Project, and then type ChangeTitleWCFProject as the name of the project. Click OK.
In the SharePoint Customization Wizard, verify that the correct local site is specified for debugging. Because sandboxed solutions do not support WCF services, select Deploy as a farm solution, and then click Finish.
To create the external WCF project to obtain its IService1 and Service1 .cs files, click the ProjectTracker solution again, and follow the same procedure as in Step 1 to open the Add New Project dialog box. Expand Visual C# node, select WCF, select WCF Service Library, type ChangeTitleWCF as the name, and then click OK.

Copy the generated IService1 and Service1 files into the ChangeTitleWCFProject project. Because you no longer need the WCF Service Library project, you can remove it from the solution by right-clicking the WCF Service Library and clicking Remove.

Add references in the ChangeTitleWCFProject to the System.Runtime.Serialization and System.ServiceModel WCF assemblies, and to Microsoft.SharePoint, the main assembly of the server object model. Right-click the ChangeTitleWCFProject, click Add Reference, and select each of these assemblies on the .NET tab.

To add a reference to Microsoft.SharePoint.Client.ServerRuntime, which contains the service factories that are provided by SharePoint Foundation, use the Browse tab of the Add Reference box to navigate to the Microsoft.SharePoint.Client.ServerRuntime.dll file in %Windows%\assembly\GAC_MSIL\Microsoft.SharePoint.Client.ServerRuntime, select the DLL, and then click OK.

To specify the contract of your custom WCF service in IService1.cs replace the auto-generated service contract with the following interface definition, where the ChangTitle method accepts the name of the list in which to change the title of the item

  Now that the implementation of the service is ready, you can deploy the service to SharePoint Foundation. Right-click the ChangeTitleWCFProject project, point to Add, and click SharePoint Mapped Folder. In the Add SharePoint Mapped Folder dialog box, select ISAPI, and then click OK to map the ISAPI folder of the SharePoint Foundation hive to the ChangeTitleWCFProject project. If Visual Studio creates a ChangeTitle subfolder in the ISAPI folder of the ChangeTitleWCFProject, right-click the subfolder and click Remove to delete it.
 To create a registration file for your service in the ISAPI folder, click the ISAPI folder in your project, and on the Project menu, click Add New Item. Under Installed Templates, select General. Select Text File, name the file Revert.svc, and then click Add.

Add the following service declaration to ChangeTItle.svc, which specifies the SharePoint Foundation factories and the namespace that contains them. In the example, MultipleBaseAddressBasicHttpBindingServiceHostFactory specifies the service factory for the SOAP type of web service. The service class declaration also specifies the name of the service class and uses a token to specify the strong name of the assembly.

<%@ServiceHost Language="C#" Debug="true"
    Service="WcfService.ChangeTitleService, $SharePoint.Project.AssemblyFullName$"
Microsoft.SharePoint.Client.ServerRuntime, Version=, Culture=neutral, PublicKeyToken=80e7sde143e94929r" %>

After you add the previous tag, save the project and close the .csproj file. In Solution Explorer, right-click the ChangeTitleWCFProject project, and then click Reload Project.

 To deploy the custom web service to SharePoint Foundation, in Solution Explorer, right-click the ChangeTitleWCFProject project, and then click Deploy. Visual Studio compiles the project’s code, builds a WSP file, and deploys the file to the front-end web server.

  To use the custom web service from your ProjectTracker client application, right-click the Service References node of the application in Solution Explorer, and then click Add Service Reference. In the Add Service Reference dialog box, type the URL of your custom WCF service in the Address box, and specify MEX as the standard name for the metadata exchange endpoint, as follows: http://Server/sites/SiteCollection/MyWebSite/_vti_bin/Revert.svc. Click Go to download the service information, and then click OK to add the reference.

  Double-click the Change button and add to its Click event the following standard WCF proxy setup code with a call to your custom WCF service. Resolve references to assemblies by right-clicking red underlined elements in the code, pointing to Resolve, and accepting recommended assembly references for the System.ServiceModel namespace and for the namespace of your custom WCF service (ProjectTracker.ServiceReference2).
This is how it’s get used in the separate application.

No comments: