Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in...

15
Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Applies To: Microsoft Visual Studio 2005 Microsoft .Net Framework 1.1 Access to SAP NetWeaver Exchange Infrastructure 3.0 (SAP employees can use X3A) Summary The aim of this article is to explain the method to consume a Web Service using Windows form Visual Studio 2005. Please note that this is a technical document and I have used a small web service to demonst implementation. Prerequisite: Beginner level knowledge of Visual Studio 2005 and C# and Object oriented conc By: Vikas Aggarwal Company: SAP Labs India Date: 22 March 2006 Table of Contents Applies To:................................................................................. Summary .................................................................................... Table of Contents .......................................................................... Scenario ................................................................................... Adding Web Service to Visual Studio Project ................................................ Additional Notes ........................................................................... Author Bio.................................................................................. Disclaimer & Liability Notice............................................................... © 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 1

Transcript of Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in...

Page 1: Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Scenario Employee details need to be fetched based on Employee

Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#)

Applies To:

Microsoft Visual Studio 2005 Microsoft .Net Framework 1.1 Access to SAP NetWeaver Exchange Infrastructure 3.0 (SAP employees can use X3A)

Summary

The aim of this article is to explain the method to consume a Web Service using Windows forms in Microsoft Visual Studio 2005.

Please note that this is a technical document and I have used a small web service to demonstrate the process of implementation.

Prerequisite: Beginner level knowledge of Visual Studio 2005 and C# and Object oriented concepts.

By: Vikas Aggarwal

Company: SAP Labs India

Date: 22 March 2006

Table of Contents

Applies To:........................................................................................................................................1

Summary ..........................................................................................................................................1

Table of Contents .............................................................................................................................1

Scenario ...........................................................................................................................................2

Adding Web Service to Visual Studio Project ..................................................................................3

Additional Notes .............................................................................................................................10

Author Bio.......................................................................................................................................15

Disclaimer & Liability Notice...........................................................................................................15

© 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 1

Page 2: Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Scenario Employee details need to be fetched based on Employee

Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#)

Scenario

Employee details need to be fetched based on Employee ID from a backend R/3 system using a web service.

Create a Web Service in an R/3 system and note the URL to the WSDL file.

URL for WSDL used in this case:

http://pwdf2813.wdf.sap.corp:50031/sap/bc/srt/xip/sap/EmplQueryResponse_In?sap-client=100&wsdl=1.1

The Web Service generates the output as shown in the following web browser screenshot.

© 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 2

Page 3: Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Scenario Employee details need to be fetched based on Employee

Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#)

Adding Web Service to Visual Studio Project

Create a new Windows application project: “EmployeeInfo”.

Create a Windows form (EmployeeQueryForm) as shown below with the required fields. Give meaningful names to all fields.

Select option Menu >> Project >> Add Web Reference.

Enter the URL in the field as shown below and select Go button

© 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 3

Page 4: Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Scenario Employee details need to be fetched based on Employee

Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#)

Enter User id and password for the server when asked.

© 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 4

Page 5: Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Scenario Employee details need to be fetched based on Employee

Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#)

Enter the same User id and password once more when asked.

Details of Web service will be fetched.

© 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 5

Page 6: Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Scenario Employee details need to be fetched based on Employee

Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#)

Enter a name for the service and select “Add Reference”.

Visual Studio will create a proxy for the web service and place it the namespace “EmployeeQueryService”. You can see the classes and parameters created in the Object Browser.

Open EmployeeQueryForm.cs code in text editor.

Add the following using directives at the top of the code.

using EmployeeInfo.EmployeeQueryService;

using System.Net;

The list of methods is available in the object browser.

© 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 6

Page 7: Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Scenario Employee details need to be fetched based on Employee

Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#)

Now our task is to call this method with right parameters just like any other method call and populate the required fields with the return parameters.

Enter the following code in the click event of the Submit button.

String EmplID;

String EmpName;

String EmpEmail;

String EmpPhoneNumber;

EmplID = textEmplID.Text;

// Instantiate the Webservice

EmplQueryResponse_InService EmployeeQueryService = new EmplQueryResponse_InService();

© 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 7

Page 8: Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Scenario Employee details need to be fetched based on Employee

Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#)

try

{

// Pass Login details to access the Web Service

NetworkCredential credentials = new NetworkCredential("AGGARWALVI", "password");

EmployeeQueryService.Credentials = credentials;

// Call the web service

EmployeeQueryService.EmplQueryResponse_In(ref EmplID, out EmpName, out EmpEmail, out EmpPhoneNumber);

// Populate the return parameters in the text fields

textEmpName.Text = EmpName;

textEmpPhoneNumber.Text = EmpPhoneNumber;

textEmpEmail.Text = EmpEmail;

}

// Exception handler

catch (Exception ex)

{

MessageBox.Show("Error fetching Employee details: " + ex.Message, "Error", MessageBoxButtons.OK,

MessageBoxIcon.Error);

}

Now you can compile and run the program to get the details as follows.

© 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 8

Page 9: Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Scenario Employee details need to be fetched based on Employee

Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#)

Click the Submit button.

© 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 9

Page 10: Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Scenario Employee details need to be fetched based on Employee

Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#)

Additional Notes

While adding a web service to the project, it might happen the WSDL is read incorrectly and some of the parameters are missing in the generated proxy code. Proxy code can be found in the file Reference.cs

In this case you will have to change the proxy code manually. Please note that changing the proxy code manually may cause incorrect behavior, if not handled carefully. If anything goes wrong, web reference can be updated and all changes made will be reset.

For example, in this case, Employee Name is missing in the output parameters.

Drill down to the method EmplQueryResponse_In and make the following changes.

The missing parameters will be listed above the method implementation.

Code Before change:

[return: System.Xml.Serialization.XmlElementAttribute("EmpName", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]

public string EmplQueryResponse_In([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] ref string EmplID, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] out string EmpEmail, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] out string EmpPhoneNumber)

{

object[] results = this.Invoke("EmplQueryResponse_In", new object[] {

EmplID});

EmplID = ((string)(results[1]));

EmpEmail = ((string)(results[2]));

EmpPhoneNumber = ((string)(results[3]));

return ((string)(results[0]));

}

Code After change:

public string EmplQueryResponse_In([System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] ref string EmplID, [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] out string EmpName,

© 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 10

Page 11: Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Scenario Employee details need to be fetched based on Employee

Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#)

[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] out string EmpEmail, [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] out string EmpPhoneNumber)

{

object[] results = this.Invoke("EmplQueryResponse_In", new object[] {

EmplID});

EmplID = ((string)(results[1]));

EmpName = ((string)(results[2]));

EmpEmail = ((string)(results[3]));

EmpPhoneNumber = ((string)(results[4]));

return ((string)(results[0]));

}

Drill down to the class implementation EmplQueryResponse_InCompletedEventArgs and make the following changes.

Code before change:

public partial class EmplQueryResponse_InCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {

private object[] results;

internal EmplQueryResponse_InCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :

base(exception, cancelled, userState)

{

this.results = results;

}

/// <remarks/>

public string Result {

get {

this.RaiseExceptionIfNecessary();

© 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 11

Page 12: Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Scenario Employee details need to be fetched based on Employee

Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#)

return ((string)(this.results[0]));

}

}

/// <remarks/>

public string EmplID {

get {

this.RaiseExceptionIfNecessary();

return ((string)(this.results[1]));

}

}

/// <remarks/>

public string EmpEmail {

get {

this.RaiseExceptionIfNecessary();

return ((string)(this.results[2]));

}

}

/// <remarks/>

public string EmpPhoneNumber {

get {

this.RaiseExceptionIfNecessary();

return ((string)(this.results[3]));

}

}

}

Code after change:

public partial class EmplQueryResponse_InCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {

© 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 12

Page 13: Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Scenario Employee details need to be fetched based on Employee

Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#)

private object[] results;

internal EmplQueryResponse_InCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :

base(exception, cancelled, userState) {

this.results = results;

}

/// <remarks/>

public string Result {

get {

this.RaiseExceptionIfNecessary();

return ((string)(this.results[0]));

}

}

/// <remarks/>

public string EmplID {

get {

this.RaiseExceptionIfNecessary();

return ((string)(this.results[1]));

}

}

/// <remarks/>

public string EmplName

{

get

© 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 13

Page 14: Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Scenario Employee details need to be fetched based on Employee

Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#)

{

this.RaiseExceptionIfNecessary();

return ((string)(this.results[2]));

}

}

/// <remarks/>

public string EmpEmail {

get {

this.RaiseExceptionIfNecessary();

return ((string)(this.results[3]));

}

}

/// <remarks/>

public string EmpPhoneNumber {

get {

this.RaiseExceptionIfNecessary();

return ((string)(this.results[4]));

}

}

}

© 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 14

Page 15: Cookbook: Consuming a SAP Web Service in Visual … · Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#) Scenario Employee details need to be fetched based on Employee

Cookbook: Consuming an SAP Web Service in Visual Studio 2005 (C#)

Author Bio

I am working as a Development Specialist at SAP Labs India. I have an engineering degree in Electronics & telecommunication from Mumbai University. I started my career at Infosys Technologies Ltd. In July 2003. I joined SAP Labs in May 2004. During this period at SAP, I have worked on Adobe forms and ESA enabling and Web Service creation for ERP Operations Area.

Disclaimer & Liability Notice

This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade.

SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk.

SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.

© 2006 SAP AG The SAP Developer Network: http://sdn.sap.com 15