Show TOC

Working with SAP Service Reference to Generate Proxy ClassesLocate this document in the navigation structure

Context

The GWM Visual Studio Add-In generates proxy and extension classes for an OData service in any Visual C# project. As a developer, you can use the proxy and extension classes generated by the SAP Service Reference feature to execute operations of an OData service, for example, retrieving contacts.

You add the SAP Service Reference in the following ways:
  • Using the Browse SAP Services option on the Tools menu: When the service URL is not known, select the System Type from the drop down, provide system specific authentication details, and then explore the services in the SAP Gateway system and select the required service.
  • Using the Add Services option: This option allows you to add any OData service. A well formed URL of the service should be provided. Authentication is required to access the SAP Gateway system.
    Note The format of the service URL is based on the system type selected:
    • Gateway: "http(s)://<Host>:<Port>/sap/opu/odata/iwbep/GWDEMO/"
    • SAP Mobile Platform (SMP): "http(s)://<SMPServer>:<Port>/<AppID>" or "http(s)://<SMPServer>:<Port>/<AppID>/<ConnectionName>"

    You must have the connection details and system specific authentication details to connect to the required OData service. Contact your system administrator for the appropriate details

The example below illustrates how you can add an SAP Service Reference to a Visual Studio project:

Procedure

  1. Open Microsoft Visual Studio.
  2. Choose New Project.
    The New Project window is displayed.
  3. Choose from the Visual C# node, Start of the navigation path Windows  Next navigation step  Windows Forms Application End of the navigation path.
  4. Enter a name for the project.
  5. Select a preferred location for the project to be stored.
  6. Optionally, specify a separate name for the solution. The solution name is the same as the project name by default.
  7. Click OK.
    The Windows Forms Application screen appears.
  8. In the Solution Explorer, right click on your new project name under the solution name, and select Start of the navigation path Add SAP Service Reference  End of the navigation path.
    The Add SAP Reference window appears.
  9. Select the System Type from the dropdown menu as Gateway (NetWeaver Gateway) or SMP (SAP Mobile Platform). Choose Add Services to add a service from the SAP Gateway system.
    The SAP Gateway Service Explorer window appears.
  10. Enter the URL of the service you wish to add in the Service URL field and click GO. Provide system specific authentication details if prompted.
    The selected service's metadata is retrieved and presented for verification. Once verified, the SAP Gateway Service Explorer window is loaded with the collections of the selected service. The proxy and the extension classes are generated in the Solution Explorer region under Start of the navigation path Windows Forms Application  Next navigation step  References End of the navigation path SAP Service Reference for the selected service.

The following references are added to the References folder:

  • SAP.IW.GWM.Common
  • SAP.IW.GWM.OData.Parser
  • SAP.IW.GWM.SSO

The SAP Service Reference folder is created, and proxy and extension classes are added.

Note The names for proxy and the extension classes differ depending on the selected OData service. For example, when a Workflow service is selected, the following classes are created:
  • WFSERVICE.cs
  • WFSERVICEExtension.cs
  • WFSERVICESAPExtension.cs

  1. Using these proxy classes and the extension classes, you can fetch a list of employees from the SAP system. Below is a sample program:
    Create a Windows Forms Application and generate SAP Service Reference as detailed above for the service GWDEMO to fetch a list of employees.
    1. Open the Toolbox.
    2. Drag and drop the DataGridView element from the Toolbox to the Windows Forms Application.
    3. Select the Data Grid, open its Properties view and give it a name. For example, dataGridView1.
    4. Add a button to trigger the OData Query call. For example btGetData.
    5. Double-click on the button to automatically navigate to the method btGetData_Click(), which is triggered upon a click on the button.
    6. Add the required coding to the button-click event. The method here is called btGetData_Click().
    7. Copy and paste the following code into your method.
      private void btGetData_Click(object sender, EventArgs e)
              {
                  List<GWDEMO.BusinessPartner> businessPartnerList = new List<GWDEMO.BusinessPartner >();
                  try
                  {
                      Logger.Log(Severity.Verbose, Categories.ServiceCall, " BusinessPartnerWindowsFormApp::btGetData_Click()");
                      ApplicationConfigReader.ConfigFilePath = new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath;
                      ServiceDetails serviceDetail = SAP.IW.GWM.Common.Configuration.ConfigurationReaderHandler.Instance.GetServiceDetails("GWDEMO");
                      GWDEMO.GWDEMO serviceContext = new GWDEMO.GWDEMO(new Uri(serviceDetail.Url));
                      Logger.Log(Severity.Verbose, Categories.ServiceCall, “Fetching data from SAP system");
                      foreach (GWDEMO.BusinessPartner businessPartner in serviceContext.BusinessPartnerCollection)
                      {
                          contactsList.Add(businessPartner);
                      }
                      this.dgvBusinessPartner.DataSource = businessPartnerList;
                  }
                  catch (Exception ex)
                  {
                      Logger.LogException(Severity.Error, Categories.ServiceCall, ex);
                      Logger.Log(Severity.Error, Categories.ServiceCall, "Failed to fetch data from SAP system");
      
                  }
              }
      
      This is a summary of what the code is doing:
      1. The file path to the App.config file is determined.
      2. The GetServiceDetails() method reads the App.config file and helps to retrieve the OData service URL.
      3. The service context for your OData service is prepared. Here, the URL of the OData service is required.
      4. The Query operation for the BusinessPartnerCollection is triggered and the results are processed in the foreach() loop.
        Note If you chose a Proxy Name other than GWDEMO, you need to adjust the code accordingly.
    8. Save the Project.
    9. If you selected BASIC as your authentication mode, perform the steps below:
      1. Before executing the application, ensure to set the authentication type to be used in the App.config file.
      2. Open Solution Explorer, and expand the Project folder and navigate to the Business Entity folder.
      3. Open the file BusinessConnectivityHelper.cs.
      4. Search for the method HandleSAPConnectivity.
      5. Provide the user credentials as shown in the sample code below:
        switch(authenticationType) 
             { 
               case AuthenticationType.BASIC: 
                 	webRequest.Credentials = new System.Net.NetworkCredential("username","password"); 
                     break;  
        
             }
        
        
    10. Execute the application (press F5).
    11. Click the Refresh button, for example, in the Windows Form Application.

      The list of employees are fetched and displayed in the grid.

    Recommendation When you add an SAP Service Reference to your existing project that already contains an app.config file, merge your original app.config with the one generated by the SAP Service Reference feature.