Working with SharePoint 2010 Object Model using Custom WCF Service

Posted by: Mahesh Sabnis , on 9/9/2011, in Category SharePoint
Views: 105702
Abstract: In this article, we will see how to use a custom WCF service to isolate the core SharePoint 2010 Object Model 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.

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.

 

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
over here

Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Nicole on Saturday, October 1, 2011 9:28 AM
Hi,

Really great and informative article! I would also like to do a similar project with what you did. I would like to create a WCF service that will communicate with SP server to create sites, get sites, etc. However, when I'm changing the target platform of Visual Studio 2010 to .Net 3.5, it fails to add the service, yielding an error "The type 'SPAPIWCF.Service1', provided as the Service attribute value in the ServiceHost directive could not be found." When I navigate to the URL of the service and click on the service name, it states that it could not load the assembly of the project because of "The exception message is: Could not load file or assembly 'SPAPIWCF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format..]"
  
When the platform is .net 4, I'm able to test the service; however, the code does not work. I think this is because SP runs on .NET 3.5. Do you have any idea how to solve this issue?


Thank you very much!
Comment posted by Avinash Reddy on Monday, October 15, 2012 11:15 AM
Hi Mahesh,
  
       Its a wonderful article !! Can you let me know what are benefits of using this model and when should we use it. Thanks in advance
Comment posted by Anonymous on Monday, October 22, 2012 6:25 AM
code not working
Comment posted by Anonymous on Monday, October 22, 2012 6:29 AM
Could not load file or assembly 'WCF_SPS_2010_List_Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Comment posted by Gopichand on Saturday, March 30, 2013 12:55 AM
Its not working with any as target platform. Its giving error when trying access SharePoint server objects.

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel