Working with SharePoint 2010 Object Model using Custom WCF Service
Posted by: Mahesh Sabnis
in Category SharePoint
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:
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.
In the application we will develop, I have used the following 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:
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:
Step 5: Open the Service.svc.cs and write the following code:
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:
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:
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.
Step 12: Run the application, Enter data in the TextBoxes and click on the ‘Save Product’ button, the result will be as shown below:
You can check the newly added entry in the List by refreshing the SharePoint 2010 site, as shown below:
The Need for SPUserToken.SystemAccount
Open the Service.svc.cs file and change the following code from:
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))”
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
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!