You are on page 1of 27

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

Applies to:
SAP NetWeaver 2004, SAP XI/PI, Microsoft Visual C # 2008 SP1 Express Editions, Microsoft .Net Framework 3.5 SP1

Summary
The purpose of this article is to demonstrate how to consume a web service created in the SAP eXchange Infrastructure (XI) using Visual Studio c# Express Edition, to help developers and analysts to leverage the Service Oriented Architecture (SOA) through of the use of these technologies. Author: Gilmar Luiz Puntel Jnior

Company: BRF Brasil Foods S.A. Created on: 17 June 2010

Author Bio
Gilmar Luiz Puntel Jnior has 12 years of experience working with SAP system. 3 years as Support Analyst and 6 years working as Business Analyst at Brasil Foods S/A, in 3 projects of SAP R/3 implementation, as member of SD team. Education: Bachelor in System Information, Technical in Information Management, SD SAP Consultant Certified ECC 6.0 and ITIL Foundation V2 Certified. The product of the last project used Sap Netweaver technologies, and this article is part of this project.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 1

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

Table of Contents
Understanding Web Services..................................................................................................................... 3 Business Scenario ..................................................................................................................................... 3 SAP R/3 Side ............................................................................................................................................ 4 SAP XI Side .............................................................................................................................................. 4 Microsoft Visual C# 2008 Express Edition .................................................................................................. 4 Creating the Visual Part of the Application ................................................................................................. 5
1) Create New Project.................................................................................................................................................. 5 2) Rename Windows Form .......................................................................................................................................... 9 3) Creating labels to our parameters ............................................................................................................................ 9 4) Creating Text Box for our parameters: ................................................................................................................... 11 5) Creating a check-box to access through a proxy: .................................................................................................. 13 6) Creating button to call the Web Service: ................................................................................................................ 15 7) Grid to view the return of the web service: ............................................................................................................. 17

Creating the Coding of the Application ..................................................................................................... 20


1) Classes to consume the web service ..................................................................................................................... 20 2) Populate our web service request .......................................................................................................................... 21 3) Call web service ..................................................................................................................................................... 22 4) Populating the Data Grid View ............................................................................................................................... 23 5) DataGridView after execution ................................................................................................................................ 24

Conclusion .............................................................................................................................................. 25 Related Content ...................................................................................................................................... 26

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 2

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

Understanding Web Services


Web services, how the proper name says, are softwares available on internet, and used to execute a specific job. They execute a specific job, so they are called services. There are a lot of web services available at internet today, with the more variables objectives, from more simple logic to more complex: convert a temperature, get the conversion of a currency, create a purchasing order or control a warehouse for example. This way, we have a side that are the consumer of a service and the provider of a service. The objective of a web service, is to become available to other softwares your service, transforming the IT environment interoperable. To become this possible, web services have many rules (open standards), that are maintained by a consortium called W3C (www.w3c.org), that define these rules. Here are the more important concepts behind a web service: XML: eXtensible Markup Language. Its important to observe that all information exchanged from consumer to provider or from provider to consumer are based on XML. XML documents are separated by tags, and is been used in almost all softwares that needs to exchange information thought enterprises. WSDL: Web Service Description Language. This is a XML document that describes what will be sent to a provider, what will return like answer and how this service can be accessed. SOAP: Simple Object Access Protocol. Its a communication protocol written in XML that is conventioned in all web service structure. A SOAP message is sent to a provider, and this provider return a SOAP message too. UDDI: Universal Description Discovery and Integration. This is an specification used to describe and discover web services.

All these concepts, are controlled by versions. Its important see all the modifications made by W3C in these documents to avoid problems in integration of environments. Here, we have a little explanation of the concepts associated to web services, only to put the reader in the context of the examples that we will show here. Business Scenario In this chapter, will be explained which business scenario are involved the solution created using web services concepts. In our company, and others too, we need to exchange information with third party softwares, that sometimes are our business partners. In most cases, the information is in a file that has a text format, and is maintained in a file server, with a standard root directory. These third party software, need read or write files in our file server. When this third party software writes a file in the server, a customized program in SAP R/3 will read this file to execute a business operation that can be a sales order, purchasing order, and so on. The information that goes to this third party software, is written by a customized program in SAP R/3 too, in a file in the same standard root directory. To better understand the technical solution created to this business scenario, we will divide this article in SAP R/3 side, SAP XI side and consumer side.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 3

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

SAP R/3 Side In the SAP R/3 was developed a function that reads the standard directory from a file server, that maintain these files. To have the reference of the root directory, was created a customized table that saves the root directory of the file server. For each kind of information, for example, customers, materials, price, master data, there is a specific nomenclature of the file that indicate his name in the file server, and this information is maintained in a customized table too, associated with a type of file, that indicates what kind of information contain the file, or what kind of information will be written in the file server. The parameters of this function (import) are the following: Mailbox: this parameter, concatenated with the root directory that is saved in the customized table, gives the directory to find the file. File_Type: this parameter, indicate the file that will be read/write in the directory. Event: this parameter indicates if a read or writes operation will be executed in the file server. t_data_in: this parameter receives the information sent by the third party software, which will become a file, in the file server. The return of this function (export) is the following: t_data_out: this is an internal table where each line, represents a line of the file. SAP XI Side The web service developed in the SAP XI has the same parameters of the function. To connect SAP XI with R/3 system, its used the RFC adapter, that executes the mapping from the message received in the XI to a valid RFC call, and executes the function in the R/3 system. Only these details will be showed here, because the focus of this article is to consume SAP XI web service from Visual Studio. More details about RFC adapter, can be found in this link: RFC Adapter Documentation. How was explained in the begin of this article, to develop our application, we will need the description of the service, that is in the WSDL file. I do not have access to find the WSDL through a UDDI of our server. So, the developer that developed this web service, needs to download a WSDL into a file, that will be imported in the execution of our project. Microsoft Visual C# 2008 Express Edition Visual Studio is one tool used to develop softwares to Windows platform. The download of the application can be done here: Download Microsoft Visual C# 2008 Express Edition. To consume SAP XI Web Service, will be developed a Windows Form application. The objective of the application is to consume the web service created in the SAP XI. In the next steps, we will show in Visual Studio how to create a project and import the WSDL that will be our reference to construct our application.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 4

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

Creating the Visual Part of the Application 1) Create New Project Step 1 Create New Project. Open Visual Studio C# 2008. Go to Menu: File -> New Project.

Step 2 Naming Windows Form Application Choose Windows Forms Application. In the field Name type the name of your project. We will call in this article TestWebService. After, click on OK.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 5

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

Step 3 Adding Web Reference This is a very important step of our project. Here is the concept of interoperability coming true. The WSDL file available by the developer of web service will be imported here like a Web Service Reference. Lets see how this work: Go to Menu: Project -> Add Service Reference.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 6

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

In the next screen, click on Advanced button.

The next screen, click on Add Web Referencebutton.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 7

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

In the next screen, in the URL field, type the directory where WSDL file was saved, that can be a network directory, a local machine directory or the URL of the XI Server if available. After type the directory, click on Go button. If everything is ok, Visual C# will find a service that is described on WSDL file. In Web reference name: type the name of the web reference. This name will be used in our project to create instances of the class that Visual C# will create with reference to WSDL file, and in this project we will call WebServiceXI. In the next screen, click on Add Reference.

After include our web reference, in the Solution Explorer our web reference will appear.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 8

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

Step 4 Building Windows Form Application These steps are needed to create an application that will help the reader to understand how we will consume the web service in Visual C#, so, lets create an application with the most details as possible. Here, I will explain how to create our Windows Form Application to consume the web service. 1) Rename Windows Form First, rename the Windows Form: Click on Windows Form, and in the tab Properties set the property Text with Testing Web Service.

2) Creating labels to our parameters Our application will have the same parameters that were created in our web service. So, I will input some labels to name our parameters. In tab Toolbox, navigate to node All Windows Forms tab, search for Label. Drag-and-drop the control to our form, like this:

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 9

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

In the Properties tab, change the property Text to the name of our first parameter, called Mailbox.

Do the same step to the next parameters that will be used to consume the web service: Event File Type

In the end of this step, we will have an application similar with this:

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 10

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

3) Creating Text Box for our parameters: Like we inserted the labels to our parameters, lets do the same thing with our texts boxes: Select Text Box in toolbox tab and drag-and-drop our parameter to our form.

For this control, we will not change the text, but we will change an important property, called (Name). Select your text box and go to Properties tab. Search for (Name) property and change to ParamMailbox. This name will identify our object when we will code the application.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 11

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

Do the same step to the next parameters that will be used to consume the web service: Event (Property (Name): ParamEvent); File Type (Property (Name): ParamFileType);

In the end of this step, we will have an application similar with this:

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 12

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

4) Creating a check-box to access through a proxy: If your application will run under a proxy to access the internet, we need to set a user and password to have access to internet. To do this (the code will be explained in the next steps), we will create a check-box called Proxy Server Access. In the tab Toolbox, navigate to node All Windows Forms tab, search for CheckBox. Drag-and-drop this control to the application.

Select your check box and go to Properties tab. Search for (Name) property and change to ProxyAccess. This name will identify our object when we will code the application.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 13

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

Still in the Properties tab, change the property Text to the name of our first parameter, called Mailbox.

This check box is to facilitate the use of this application, and to understand in the code in what moment the proxy server, user, password and domain will be set in the application.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 14

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

5) Creating button to call the Web Service: In this step we will create a button that will execute a call to our web service. Like the others, this is very a simple step. In the tab Toolbox, navigate to node All Windows Forms tab, search for Button. Drag-and-drop this control to the application.

Select your button and go to Properties tab. Search for (Name) property and change to CallWebService. This name will identify our object when we will code the application.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 15

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

Still in the Properties tab, search for Text property. Change this property to Call Web Service!. After these steps, we will have a screen like this:

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 16

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

6) Grid to view the return of the web service: Well, we need to see the return of the web service, and, to do this, we will insert a data grid to show the return of the web service. In the tab Toolbox, navigate to node All Windows Forms tab, search for Button. Drag-and-drop this control to the application. In the window that will open, click on Edit Columns.

In the next screen that will appear, click on Add.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 17

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

In the next screen, type t_data_out that will be the name of the column in the field Name:, and the header text of the column in Header text:. Set the check box Visible and Read Only, to set the column to do not be editable.

Click on Add and then Close. In the next screen, click on OK.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 18

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

In the next screen, resize the data grid. Click on the data grid, and change the property Name, typing WebServiceReturn.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 19

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

Creating the Coding of the Application To input the code in the application, give a double click in the button Call Web Service!. We will go to a screen like this:

To better understand the code, I will insert in the code in little pieces and with comments to better understand. To each co-relational piece, I will explain the code. In the end of this article, the written code will be available to copy-and-paste. Well, lets begin. 1) Classes to consume the web service First we will instantiate the class with the interface of the web service, the class to do a request to the web service and the class to receive the return of the web service. Its important to say that these classes were created automatically when we imported the WSDL to our coding. The same way occurs when we import a WSDL to the XI or another development tool like NetBeans, to consume a web service that is in the internet for example. Of course that in the XI there are a visual interface to create a communication channel, and we will don't see the code created.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 20

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

2) Populate our web service request In this step, we will pass the parameters of our windows form application to the class that is responsible to maintain the parameters of our web service request. Beyond to pass the parameters from our application, we need to instantiate another class that has the objective of to set the user and password to connect to the XI server. Change the string user and password to your user and password of your SAP XI server. Like I sad in the step 4.4, we will input a code to treat the check-box Proxy Access ?. If the check-box is checked, so we will instantiate another class to pass to WS_ITF the user, password and domain if applicable. Change the string user, password and domain to your user, password and domain of your proxy server.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 21

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

3) Call web service In this step, we will pass the class request (WS_REQ) to the class interface (WS_ITF). The class web service return (WS_RET) will be responsible to receive the response of the web service. Here we will have a call to the URL of our XI Web Service. The URL is an item of the WSDL. This way of interoperability is simply fantastic because we have a pattern, and in theory all the development tools that are able to work with XML and web services can understand what the provider of the service needs to receive to execute the service and what kind of information it will return. Below we can see the simplicity of placing a call to web service.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 22

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

4) Populating the Data Grid View In this step, we will populate the data grid with the return of the web service.

This way we receive the return of the web service and populate the data grid view.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 23

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

5) DataGridView after execution Here we can see the return of the web service in our data grid view, obviously with fictitious data. So, we user a mailbox called 0197, that is a directory of our unix server, and the file type CUS (customer) to simulate a file that contains customer data, that can be used for a great range of applications, mobile in our solution for example.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 24

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

Conclusion As we saw in this article, web services are showing yourself as a very important piece of the Service Oriented Architecture. Like I've wrote in the beginning of this article, web services are just a little piece of the whole architecture. In the internet, web services have been used in a great portion of the distributed applications. The web service created to attend the company needs, created a new vision of the interfaces used today, that can be migrated to the SOA concept, to be better managed and reusable for other applications. The files that are exchanged every day in our company (a considerable quantity) can be easily changed to a web service with the help of the SAP Exchange Infrastructure tecnologhy. When a software can be developed and delivered with the aid of the tecnologies like web services and XML, we can affirm that it will decrease TCO of IT, principally because of the reusablity and interoperability of these services, as showed here using Visual Studio, that by the way is a very good tool. But the concepts of XML are used in almost all tools of development. All the biggest IT companys are investing in researches about SOA, and we can affirm that this is a way without comeback, and with sure, will help all the IT environment of the companies, and the IT staff.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 25

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

Related Content Overview of Sap NetWeaver 2004 Integrating Remote Client/Web Services by Using the SOAP Adapter Microsoft Developer Learning Center SAP Community Network SOA Topic

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 26

Consuming SAP XI Web Service with Microsoft Visual C# 2008 Express Edition

Copyright
Copyright 2009 SAP AG. All rights reserved. No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation. IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9, iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Serv er, PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER, OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered trademarks of IBM Corporation. Linux is the registered trademark of Linus Torvalds in the U.S. and other countries. Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other countries. Oracle is a registered trademark of Oracle Corporation. UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc. HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C, World Wide Web Consortium, Massachusetts Institute of Technology. Java is a registered trademark of Sun Microsystems, Inc. JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by Netscape. SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP Business ByDesign, and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries. Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects S.A. in the United States and in other countries. Business Objects is an SAP company. All other product and service names mentioned are the trademarks of their respective companies. Data contained in this docume nt serves informational purposes only. National product specifications may vary. These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("S AP Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable fo r errors or omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 27

You might also like