Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Example of Windows Phone 7 Silverlight Application connected to SAP system

0.00/5 (No votes)
16 Jan 2013 1  
How to connect your application phone to SAP system.

Introduction

This is my first article. I intended it for beginners (like me) in Windows Phone applications. The code shows how to create a Windows Phone application connected to a SAP system, where user can enter his login and password to open his SAP session and get his record information. For this I will use Visual Studio Express 2010 to create my Windows Phone 7 applications (note that it will be the same for Windows Phone 8, but in this case you will need VS Express 2012). I also need VS 2010 to create my web service connected to the SAP system, or we can also create a simple service like WCF. This layer will be replaced by a SAP Netweaver Gateway services in the next article. And finally, I need my SAP BAPI. They work like Function Modules, the only difference is that they are the programming interface (API) for the SAP Business Object Repository.

Background

For my SAP connection I will use ERPConnect.net, which is a .NET component that offers several classes for client and server communications with SAP R/3 systems including RFC function modules, BAPIs. Besides standard classes for RFC server and client functions there a several special classes to perform functions like create and execute ABAP code dynamically or control SAP transaction in the SAP GUI. To use ERPConnect, you need to add a reference DLL into your project. (Don’t forget to add the statement using ERPConnect to your programmed class).

Step 1 : Create My BAPI

My SAP function gets the user's login and password as parameters, and it returns a structure with user first name, last name and function. Here is my function:

Step 2 : Create a web service:

In this article I will use a web service to connect my Windows Phone application to SAP. Note that it's not the only solution as descripted in my introduction. But we need always a layer between Windows Phone and the SAP system, a direct connection still not possible up to now. So let's create my web service: Open Microsoft Visual Studio and open a new project, from the open window choose C#, Web then web service application. After creating the new project you will find the project explorer, right click and add a new class "SAPUser". This class will be our entities to export data from our web service.

public class SAPUser
{
    // déclaration des champs.
    private string _nom;
    private string _prenom;
    private string _poste;
    private string _fonction;

     // déclaration des propriétés
    public string Nom
    {
        get { return _nom; }
        set { _nom = value; }
    }
    public string Prenom
    {
        get { return _prenom; }
        set { _prenom = value; }
    }
    public string Poste
    {
        get { return _poste; }
        set { _poste = value; }
    }
    public string Fonction
    {
        get { return _fonction; }
        set { _fonction = value; }
    }
}

Before creating our web method, we must add a reference for our ERPConnect. In my example am using Visual studio 2010 with Framework 3.5 so I need to add ERPConnect35.dll to my web service project, like below:

zp>Now let’s create our web method, for this open the file "Service1.asmx.cs", and add a new method in the service. This method will take as parameter login sap and password, and it will send an object of our class SAPUser. As shown below you need to create an R3Connection object to open the connection with sap, and then we create an RFCFunction object on the connection calling our BAPI Z_GET_USER_INFO. Then we fill the Exports parameter of our RFC function object with the Import parameter of the BAPI. That means we send the Import value to the BAPI. After this is done, we execute the function and extract the data of interest from the result structure.
public class Service1 : System.Web.Services.WebService
{
    [WebMethod]
    public SAPUser GET_SAP_USER(string userName, string passwordUser)
    {
        SAPUser sapUserResult = null;
        R3Connection _con;
        ERPConnect.LIC.SetLic("XXXXXXXXX");
        _con = new R3Connection();
        _con.UserName = userName;
        _con.Language = "FR";
        _con.Client = "100";
        _con.Host = "the name of the SAP Server";
        _con.SystemNumber = 4;
        _con.Password = passwordUser;
        try 
         {  
             //Open a connection to sap server
             _con.Open(false);
             // call the bapi Z_GET_USER_INFO
             RFCFunction func = _con.CreateFunction("Z_GET_USER_INFO");
             //sending the import parameter to the BAPI 
             func.Exports["USER_NAME"].ParamValue = userName;
             func.Execute();
             RFCTable UserParameter = func.Imports["P_USER_INFO"].ToTable();
             if (UserParameter.RowCount > 0)
            {    
                sapUserResult = new SAPUser();
                sapUserResult.Nom = UserParameter.Rows[0]["NOM"].ToString();
                sapUserResult.Prenom = UserParameter.Rows[0]["PRENOM"].ToString();
                sapUserResult.Poste = UserParameter.Rows[0]["POSTE"].ToString();
                sapUserResult.Fonction = UserParameter.Rows[0]["FONCTION"].ToString();
            }
         }
        catch (Exception ex)
        { 
         return null; 
        }
        finally  
        { 
           _con.Close(); 
        }
       return sapUserResult;
    }
}//

Step 3: Create windows phone application:  

Now we need to create a windows phone application to consume our web service. For this, open Microsoft Visual Studio Express 2010 and open a new project. From the open window choose Visual C# ==> "Silverlight for Windows Phone", tape the name "WPUserSAPConnection". After this you will find the project explorer. Right-click and add our web reference (to do this the web service created in step 2 must be published). In the web reference address tape WSDL address created in step 2.

Now let’s create our Windows Phone interface. For this, open the file "MainPage.xaml", and add two zones for SAP login and SAP password. For the result we need three labels: user first name, last name, function.

Open Toolbox and drag the following controls:   

  1. Button – change the text property to "Login" 
  2. Two textbox for login and password, note that for password text box the type property is password
  3. Four texts Label – 1. First Name 2. Last name 3. Poste 4. function. 

 Add Code Behind 

For my button login event click here is my code. 

private void btnLogin_Click(object sender, RoutedEventArgs e)
{
    SAPService.Service1SoapClient service = new SAPService.Service1SoapClient();
    service.GET_SAP_USERAsync(txtLogin.Text, txtPassword.Password);
    service.GET_SAP_USERCompleted += 
      new EventHandler<SAPService.GET_SAP_USERCompletedEventArgs>(GET_SAP_USERCompleted);
}
private void GET_SAP_USERCompleted(object obj, SAPService.GET_SAP_USERCompletedEventArgs e)
{
    lblFonction.Visibility = System.Windows.Visibility.Visible;
    lblNom.Visibility = System.Windows.Visibility.Visible;
    lblPoste.Visibility = System.Windows.Visibility.Visible;
    lblPrenom.Visibility = System.Windows.Visibility.Visible;
    textBlock3.Visibility = System.Windows.Visibility.Visible;
    textBlock4.Visibility = System.Windows.Visibility.Visible;
    textBlock5.Visibility = System.Windows.Visibility.Visible;
    textBlock6.Visibility = System.Windows.Visibility.Visible;
    lblNom.Text = e.Result.Nom.ToString();
    lblPrenom.Text = e.Result.Prenom;
    lblPoste.Text = e.Result.Poste;
    lblFonction.Text = e.Result.Fonction;
}

Test application

 

Result:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here