Please also have a look in our OnlineHelp for further information.
To show the potential of ERPConnect Services for mobile programming, this article shows how to create a Windows RT app for the SAP MM inventory process with focus on being used on tablet computers.
The app communicates with the SAP system through a custom WebService running on a SharePoint system. The web service was created with component WebService Designer as part of ERPConnect Services.
This article will focus on how to communicate with the SAP system using that web service. With regard to better readability, this article does not include surrounding parts of app development like creating the UI.
The example solution is available via this link. The web service used for this example is available at this site. Just add it to the WebService Designer to deploy and explore it.
Visual Studio allows developers to easily integrate web services in their products. By adding the reference, it will automatically create the classes that are necessary to interact with the web service.
To do so, go to Solution Explorer and right click on References. In the opening context menu click Add Service Reference and a new window will open. Enter the address to your web service and click the button labeled with Go. While scanning the URL for web services, you will be asked to enter your SharePoint credentials.
The result of the scan should be the SAPMMInventory web service in the left field. Select it, enter an appropriate name in the namespace textfield at the bottom of the window, in this example it is SapInventoryWebService, and click the Advanced… button. Another window will open where you should change the value for Collection Type to System.Collections.Generic.List. Close this window with a click on the OK button. The service reference is now configured and you can add it with a click on the OK button.
Once this is done, you have to add some code to the generated class. In Solution Explorer, navigate to Service References -> SapInventoryWebService -> Reference.svcmap -> Reference.cs. If you can´t expand the web service´s folder, click on Show All Files in the upper right of the Solution Explorer.
In the method private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration) add the following lines to the if branch:
result.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Basic;
result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.TransportCredentialOnly;
So the method looks like this:
private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration) {
if((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_ISAPMMInventory))
{
System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
result.MaxBufferSize = int.MaxValue;
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
result.MaxReceivedMessageSize = int.MaxValue;
result.AllowCookies = true;
result.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Basic;
result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.TransportCredentialOnly;
return result;
}
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
}
The additional lines need to be there to specify the correct authentication against the SharePoint system. Be advised: When updating the service reference, the Reference.cs file needs to be modified again, because Visual Studio overwrites it when generating the classes for the updated reference. Even if nothing has changed.
The communication with the SAP system is defined in a class called DataManager. This contains all methods for sending data to and receiving data from the remote system. In the following, the method to receive a single inventory list will be discussed in detail.
The DataManager has a property called Client. This property is an object of the class SAPMMInventoryClient that was automatically generated by Visual Studio when adding the service reference. Add a reference to the service reference classes within a using directive like this one:
using *YourProjectName*.SapInventoryWebService
In the constructor of the DataManager, the client will be defined and the necessary properties for the authentication will be added. The code looks as follows:
public DataManager() {
client = new SAPMMInventoryClient();
System.ServiceModel.EndpointAddress address = new System.ServiceModel.EndpointAddress("http://*YourServerIp*/_vti_bin/SAPMMInventory.svc");
client.Endpoint.Address = address;
client.ClientCredentials.UserName.UserName = "YourSharePointUsername";
client.ClientCredentials.UserName.Password = "YourSharePointPassword";
}
After creating the object, the address of the endpoint is added in line 3 and 4. Then, the credentials need to be added in line 5 and 6. The client object has a property for the credentials that can be used due to the modification in the Reference.cs file. This is all that is necessary for being able to connect to the web service.
To prevent the UI from freezing, network operations should run asynchronous and not on the UI thread. This is the method that is used to receive data from the web service:
public async Task<List<InventoryGetDetailsElement>> getInventoryList(string inventoryNumber, string fiscalYear) {
List<InventoryGetDetailsElement> results = new List<InventoryGetDetailsElement>();
try
{
results = await this.client.GetDetailAsync(inventoryNumber, fiscalYear);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Receiving data resulted in an error:\n {0}", ex.Message);
throw ex;
}
return results;
}
Remember to add the System.Threading.Tasks using directive when using the Task class.
In line 2 of that method, the object that will contain the results is being created. It is a generic list that will contain elements of the type InventoryGetDetailsElement that was defined in the WebService Designer. If you need further information about the WebService Designer, please have look at our OnlineHelp.
Receiving data from the web service is quiet easy: it is only that one simple call in line 5. It calls the method that was generated by Visual Studio when adding the service reference and stores the result in the results variable.
That´s it. Now the inventory data is on the local system and is ready to be processed in additional steps, for example for passing it to the UI.
The following 3 screenshots show an example inventory. The first screenshot shows the inventory detail for the material 100-100. After committing the data from the app shown in the second screenshot, you can see the results in the third screenshot.