Sunday 20 May 2012

Working with SharePoint 2010 Object Model using Custom WCF Service

The requirement behind creating a WCF service here is that the client application need not to be directly connected to the SharePoint 2010 Services. Instead an end-user carrying his laptop, will use a Desktop application to perform SharePoint List updates using WCF service.

To develop such a scenario, I have used Windows 7 Enterprise N 64 bit OS with IIS 7.5. I have SharePoint 2010 installed on the same machine.

Here I am assuming that you are using SharePoint 2010 and know how to create a Web Site collection and List etc. For the above scenario, I have a Web Site collection which contains the ‘SalesInfo’ List as shown below.

sharepoint-web-site-collection

In the application we will develop, I have used the following classes:

sharepoint-classes

Step 1: Open VS2010 and create a blank solution, name it as ‘SPS_2010_List_Custom_WCF’. In this solution add a new WCF service application (targeting to .NET 3.5), name it as ‘WCF_SPS_2010_List_Service’. In this project, add a reference to the ‘Microsoft.SharePoint.dll’.

This component is available in the following path:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\

Step 2: Rename IService1.cs to IService.cs and rename Service1.svc to Service.svc. Write the below code in IService.cs:

wcf-service

Step 3: Make sure that Service.svc markup is as below:

<%@ ServiceHost Service="WCF_SPS_2010_List_Service.
Service" Language="C#" Debug="true" CodeBehind="Service.svc.cs" %>

Step 4: Open web.config file and set the EndPoint to BasicHttpBinding as below:

basic-http-binding

Step 5: Open the Service.svc.cs and write the following code:

sharepoint-list-service

The above code makes use of the SharePoint 2010 Server object model. It opens the SharePoint 2010 site and program sagainst the List with the name ‘SalesInfo’. Please read the comments carefully for the ‘CreateSalesRecord()’ method.

Step 6: Debug the above WCF service targeting the ‘x64’ platform because SharePoint 2010 requires 64-bit support to execute.

Step 7: Publish the WCF service on IIS 7.5 and make sure that the ApplicationPool under which the applicaton is published, targets to .Net 2.0 (this also supports .NET 3.5). Also configure the Identity of the ApplicationPool to ‘LocalSystem’ as shown below:

 iis-identity-local-system

Step 8: Now publish the WCF Service by creating Application under this app pool.

Step 9: In the same solution, add a new WPF application, name it as ‘WPF_ClientWCF_SPS’. In this application, add the service reference of the WCF service. Name the namespace as ‘MyRef’.

Step 10: Add 3 TextBoxes, 3 TextBlocks and a Button on the MainWindow.xaml as shown below:

xaml-code

 wpf-client

Step 11: In the ‘Save Product’ button click event, write the following code.
 This code makes a proxy object of the WCF service and makes a  call to the CreateSalesRecord() method by passing the SharePoint 2010 site Url and the SalesInfo object to it.

save-product-button

Step 12: Run the application, Enter data in the TextBoxes and click on the ‘Save Product’ button, the result will be as shown below:

 list-item-added
You can check the newly added entry in the List by refreshing the SharePoint 2010 site, as shown below:
product-list-added 

The Need for SPUserToken.SystemAccount

Open the Service.svc.cs file and change the following code from:

run-elevated-priviledge

to

run-elevated-priviledge-1

Now publish the WCF service on IIS and Update the WCF service reference in the client application, put breakpoints on the ‘Save Product’ button click and the ‘CreateSalesRecord()’ and run the application. For debugging the WCF Service hosted on IIS, attach the w3wp.exe process. During debugging, the following exception will be displayed: “Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

 access-denied-debugging

Here the code will crash because the user object received from the client to the WCF service, will not be able to authorize itself against the SharePoint 2010 Server Object Model.

Conclusion: We have seen how to use a custom WCF service to isolate the core SharePoint 2010 services from the direct access of the remote client application. The WCF Service acts as a successful interface between the remote user client and SharePoint 2010.

The entire source code of this article can be downloaded

I have been working on a scenario where a WCF service hosted on IIS 7, communicates to the SharePoint 2010 server object model for performing List operations. Although SharePoint 2010 has provided in-built WCF services for working with List etc, but one my students who is working as a SharePoint 2010 developer, was having the following idea:

sharepoint-wcf

In the diagram, I have tried to explain the type of the application he wants to develop. The application scenario of the diagram is as shown below:
  • SharePoint 2010 Web Site Collection is hosted on IIS 7.5 on the server operating system. This collection has a Web Site which contains a List for storing Sales information. The client application does not have any direct access of the object model and services (Web and WCF) published by the SharePoint 2010 application.
  • A custom in-house WCF service is hosted on the IIS 7.5 which targets .NET 3.5. This WCF service communicates to the SharePoint 2010 Web Site using SharePoint 2010 Server Object model.
  • A desktop remote client (WPF/WinForm) communicates to the WCF service using http protocol.
over here

No comments:

Post a Comment