The introduction of Microsoft’s WCF RIA Services for Silverlight 4 simplified very much the development process of N-tier business applications using Silverlight and ASP.NET. By using this new technology, we can also easily access and integrate SAP business data in Silverlight clients.
This article shows how to provide a SAP domain service as web service that will be consumed by a Silverlight client. The sample application will allow the user to query customer data. The service uses LINQ to SAP from Theobald Software to connect to a SAP R/3 system.
The first step in setting up a new Silverlight 4 project with WCF RIA Services is to create a solution using the Visual Studio template Silverlight Navigation Application:
Visual Studio 2010 then asks you to create an additional web application, which hosts the Silverlight application. It’s important to select the checkbox Enable WCF RIA Services (see screenshot below):
After clicking the Ok button, Visual Studio generates a solution with two projects, one Silverlight 4 project and one ASP.NET project. In the next section, we will create the SAP data access layer using the LINQ to SAP designer.
LINQ to SAP
The LINQ to SAP provider and its Visual Studio 2010 designer offers a very handy way to design SAP interfaces visually. The designer will generate the code for the SAP data access layer automatically, similar to LINQ to SQL. The LINQ provider is part of the .NET library ERPConnect.net from Theobald Software. The company offers a demo version for download on its homepage.
The next step is to create the needed LINQ to SAP file by opening the Add New Item dialog:
LINQ to SAP is internally called LINQ to ERP.
Clicking the Add button will create a new ERP file and opens the LINQ designer. Now, drag the Function object from the toolbox and drop it onto the designer surface. If you have not entered the SAP connection data so far, you are now asked to do so:
Enter the connection data for your SAP R/3 system and then click the Ok button. Next, search for and select the SAP function module named
SD_RFC_CUSTOMER_GET. The function module provides a list of customer data.
The RFC Function modules dialog opens and lets you define the necessary parameters:
In the above function dialog, change the method name to
GetCustomers and mark the Pass checkbox for the
NAME1 parameter in the Exports tab. Also set the variable name to
namePattern. On the Tables tab, mark the Return checkbox for the table parameter
CUSTOMER_T and set the table and structure name to
After clicking the Ok button and saving the ERP file, the LINQ designer will generate a
SAPContext class which contains a method called
GetCustomers with an input parameter named
namePattern. This method executes a search for SAP customer data allowing the user to enter a wildcard pattern. The method returns a table of customer data:
On the LINQ designer level (click on the free part of the LINQ designer surface) property, Create Object Outside Of Context Class must be set to
Now, we finally add a
Customer class which we use in our SAP domain service later on. This class and its values will be transmitted to the Silverlight client by the WCF RIA Services. It’s important to set the
Key attribute on the identifier fields for WCF RIA Services, otherwise the project will not compile:
That’s it! We now have our SAP data access layer ready to use and can start adding the domain service in the next section.
SAP Domain Service
The next step is to add the SAP domain service to our web project. A domain service is a specialized WCF service and is one of the core constructs of WCF RIA Services. The service exposes operations that can be called from the client generated code. On the client side, we use the domain context to access the domain service on the server side.
Add a new Domain Service Class and name it
In the upcoming dialog, create an empty domain service class by just clicking the Ok button:
Next, we add the service operation
GetCustomers to the SAP service with a name pattern parameter. The operation then returns a list of
Customer objects. The Query attribute limits the result set to 200 entries.
The operation uses the visually designed SAP data access logic to retrieve the SAP customer data. First of all, an instance of the
SAPContext class will be created using a connection string (see sample in code). For more details regarding the SAP connection string, see the ERPConnect.net manual.
The LINQ to SAP context class contains the
GetCustomers method which we will call using the given
namePattern parameter. Next, the operation creates an instance of the
Customer class for each customer record returned by SAP.
The license code for the ERPConnect.net library is set in the constructor of our domain service class.
That’s all we need on the server side.
In the next section, we will implement the Silverlight client.
The implementation of the client side is straightforward. The home view contains a
DataGrid control to display the list of customer data as well as a search area with
Button controls to allow users to enter name search pattern.
The click event handler of the load button, called
OnLoadButtonClick, will execute the SAP service. The boilerplate code to access the web service was generated by WCF RIA Services in the subfolder
Generated_Code in the Silverlight project.
First of all, an instance of the
SAPContext will be created. Then, we load the query
GetCustomersQuery and execute the service operation on the server side using WCF RIA Services. If the domain service returns an error, the callback anonymous method will mark the error as handled and display the error message.
If the execution of the service operation succeeded, the result set gets displayed in the
The next screenshot shows the final result:
This article has shown how easily SAP customer data can be integrated within Silverlight clients using tools like WCF RIA Services and LINQ to SAP. It is quite simple to extend the SAP service to integrate all kinds of operations.
- 17th November, 2010: Initial post