Saturday, July 12, 2008

MS Infopath To SAP Connectivity Technical Document

Below are the steps to integrate Infopath to SAP
1. Connect to below URL to find the RFC Function Module in SAP with which your application connects
http://servername/sap(bD1lbiZjPTAxNw==)/bc/bsp/sap/WebServiceBrowser/search.html


2. I am using ‘Z_NAME_DISPLAY’ in our scenario which returns employee information when we input Employee ID as parameter. So search this RFC in R/3 Web Service Repository. It will display the output in Result box. Click on the ‘WSDL?’ link to get WSDL URL of this RFC.
WSDL is Web Service Description Language which is an XML description of a web service.

3. WSDL URL of Z_NAME_DISPLAY is http://servername/sap/bc/soap/wsdl11?services=Z_NAME_DISPLAY&sap-client=017


4. I have created a NameDisplay.xsn infopath form template.


5. Open your visual studio project associated with your infopath form. Right click on project and select ‘Add Web Reference..’


6. Enter the WSDL URL in URL box. It will ask for SAP login. After logging, it will display the name of RFC and its methods. Enter the Web Reference Name ‘NameDisplayWebService’ and click on Add Refrence.


7. WSDL utility
Now we have to use WSDL.exe utility to generate proxy file i.e. cs file of Z_NAME_DISPLAY web service. Search WSDL.exe in your system, it comes with VS2005 installation. Run below command to generate cs file:

Format > wsdl ‘username’ ‘password’ ‘WSDL URL’ ‘Name of cs file’

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>wsdl /u:lotus /p:system
"http://ServerName/sap/bc/soap/wsdl11?services=Z_NAME_DISPLAY&sa
p-client=017" /out:Z_NAME_DISPLAY.cs

It will generate below result:

Microsoft (R) Web Services Description Language Utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'Z_NAME_DISPLAY.cs'.

8. CSC utiliy
Now we will use CSC.exe utility to generate .dll file of Z_NAME_DISPLAY.cs file. Search CSC.exe in your system. Run below command

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>csc /out:Z_NAME_DISPLAY.dll /t:library Z_NAME_DISPLAY.cs /r:System.Web.Services.dll /r:System.Xml.dll

It will display below result:
Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.1433
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.


9. Again use CSC.exe to generate .netmodule file

Run below command to generate .netmodule file of Z_NAME_DISPLAY.cs file.

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>
csc /out:Z_NAME_DISPLAY.netmodule /t:module Z_NAME_DISPLAY.cs

It will display below result:
Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.1433
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

10. Strong Key Name (Sn.exe)
Now we have to generate Strong Key name to associate it with our dll file.
Sn.exe utility to generate strong key name file

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>sn -k Z_NAME_DISPLAY.snk

It will display below result:
Microsoft (R) .NET Framework Strong Name Utility Version 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
Key pair written to Z_NAME_DISPLAY.snk

11. Assembly Linker Utility (al.exe)
Now we will use al.exe to link dll file with key file

Run below command:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>
al /out:Z_NAME_DISPLAY.dll Z_NAME_DISPLAY.netmodule /keyfile:Z_NAME_DISPLAY.snk

It will display below result:
Microsoft (R) Assembly Linker version 8.00.50727.42
for Microsoft (R) Windows (R) .NET Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2003. All rights reserved.

12. Now we can put our Z_NAME_DISPLAY.dll in Gache (C:\Windows\Assembly).


13. Now open your visual studio project once again and add reference to Z_NAME_DISPLAY.dll file.



14. Add reference to ‘System.configuration’ also.


15. Also add following lines in code window at the top. Here ‘NameDisplay.NameDisplayWebService’ is NameofCSProject.WebServiceReferenceName

using System.Configuration;
using System.Reflection;
using NameDisplay.NameDisplayWebService;

16. As we have added Z_NAME_DISPLAY.dll reference, we can create an object of this class and use various properties and methods as shown below:
private Z_NAME_DISPLAYService NameDisplayService=new Z_NAME_DISPLAYService();

17. Now I have added following code on ‘Display Employee Info from SAP’ button click


public void DisplayName_Clicked(object sender, ClickedEventArgs e)
{
try
{
System.Net.NetworkCredential credential = new System.Net.NetworkCredential("ID", "password");

System.Net.WebProxy proxy = new System.Net.WebProxy("ServerName", 8010);

NameDisplayService.PreAuthenticate = true;
NameDisplayService.Credentials = credential;
NameDisplayService.Proxy = proxy;

//XML reference of fields on form
XPathNavigator EmpID = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:EmpID", NamespaceManager);
XPathNavigator PERNR= this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:PERNR", NamespaceManager);
XPathNavigator ANREX = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:ANREX", NamespaceManager);
XPathNavigator NACHN = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:NACHN", NamespaceManager);
XPathNavigator VORNA = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:VORNA", NamespaceManager);
XPathNavigator INITS =this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:INITS", NamespaceManager);
XPathNavigator FATXT = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:FATXT", NamespaceManager);
XPathNavigator ANRED = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:ANRED", NamespaceManager);
XPathNavigator FAMST = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:FAMST", NamespaceManager);
XPathNavigator NATIO = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:NATIO", NamespaceManager);

int RCODE;
ZROC_NAME[] PERSONAL=new ZROC_NAME[10];
//Call to function Z_NAME_DISPLAY() with required parameters
string test=NameDisplayService.Z_NAME_DISPLAY(EmpID.ToString(),ref PERSONAL,out RCODE).ToString();

//Set Field values on form
PERNR.SetValue(PERSONAL[0].PERNR.ToString());
ANREX.SetValue(PERSONAL[0].ANREX.ToString());
NACHN.SetValue(PERSONAL[0].NACHN.ToString());
VORNA.SetValue(PERSONAL[0].VORNA.ToString());
INITS.SetValue(PERSONAL[0].INITS.ToString());
FATXT.SetValue(PERSONAL[0].FATXT.ToString());
ANRED.SetValue(PERSONAL[0].ANRED.ToString());
FAMST.SetValue(PERSONAL[0].FAMST.ToString());
NATIO.SetValue(PERSONAL[0].NATIO.ToString());
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Error in Integrating with SAP ", ex.Message.ToString() + "--" + ex.StackTrace.ToString());
}