Magento SOAP API and C# – Part II

Posted by on

Leave a comment

Previously we expanded the Magento SOAP API to support a paginated list of customers and their default address information. Now we will consume that information in an ASP.NET MVC application.

Before We Begin

This article assumes that you have installed a copy of Visual Studio 2010 (Express) or later. Also be sure to read the previous article and complete the steps within to get your module that extends the API up and running.

Enable the cache

In the previous article I said for the sake of making debugging easier we should disable the Magento cache. I also said that at times Magento can be unbearably slow. This is possibly never truer than when trying to access Magento via the SOAP API. To maintain our sanity and decrease the chances of response time-outs, let’s turn the caching functionality back on:

  1. From the Magento Admin Panel menu, browse to ‘System > Cache Management’.
  2. Click ‘Select All’.
  3. From the ‘Actions’ drop-down choose ‘Enable’.
  4. Click ‘Submit’.

Your ‘Cache Storage Management’ settings should now look like this:

Cache Storage Management

Cache Storage Management

Our web application will also need to interrogate Magento’s WSDL which takes a significant amount of time to build so most importantly we need to enable the WSDL caching options:

  1. From the Magento Admin Panel menu, browse to ‘System > Configuration’.
  2. From the left hand menu select ‘Services > Magento Core API’.
  3. Set the ‘Enable WSDL Cache’ option to ‘Yes’.

Your ‘Magento Core API’ settings should now look like this:

Magento Core API

Magento Core API

Create our project

Begin by creating an ‘ASP.NET MVC 4 Web Application (C#)’ in Visual Studio. Then choose either the ‘Basic’ or ‘Internet Application’ template.

Add Service Reference

Once the solution has been created right click on the project and select ‘Add Service Reference’. Remember in the previous article that I mentioned that there were two versions of the Magento API. The WSDL for the two versions can be accessed from the following paths:

Version 1http://<site_url>/api/soap/?wsdl=1
Version 2http://<site_url>/api/v2_soap/?wsdl=1

Where <site_url> is the path to your Magento instance. I have created a host entry on my development machine so the path to my Magento instance is magento.local.

In the ‘Add Service Reference’ dialog use the version 2 url from above in the ‘Address’ field. Click ‘Go’. If successful the Magento service will appear in the ‘Services’ list. If you expand the service you will see all the methods that it provides including the ones we added with our module in the ‘Operations’ list. Now give the service a name. I usually use reverse domain name notation so in my case the name will be local.magento.v2. Once your dialog looks similar to the one below click ‘Ok’.

Add Service Reference

Add Service Reference

Confirm that the reference exists by expanding ‘Service References’ in the project.

Service Reference

Service Reference

Add paging controls

We implemented paging in our module so we need a matching user interface for our web project. Right click on the project again and chose ‘Manage Nuget Packages’ and install the PagedList.MVC Nuget package.

Let’s write some code

Add a new controller called ‘CustomerController’ and include the following namespace declaration. The full source of the controller is available on Github.

Notice that the namespace is our project name combined with the name we gave the service reference. Because we are using the WSI compliant version of the API we begin by creating an instance of the ‘Mage_Api_Model_Server_Wsi_HandlerPortTypeClient’ client. Next we need to authenticate against the API by calling the ‘login’ method and providing the username and password that we created in the previous article. The login method returns an API key that we will use in the remainder of our calls.

Now that we have authenticated and have the necessary API key we can start to make calls to the service. In this example will be querying Magento for customers and their address information that have a particular surname. The previous article explained that our ‘count’ and ‘pagedList’ methods expect an array of arrays. The inner arrays are AND queries and the out array is an OR query. The following code will return a list and the total number of customers with a particular surname:

Notice we pass in our API key (sessionId) to each call. Once we have finished our work we end our current session.

See the results

Create the ‘views/customer/index.cshtml’ view that matches the controller action we’ve created. The full source of the view is available on Github.  Now here’s the fun part where we get to see the fruits of our labour. Run the project. When it first runs we should get a paged list of all the customers (assuming there are some in Magento, if not go and create some). We can filter the list by surname and see the reduced set of results. Well done. You have now successfully created a Magento API module and loaded the results in an ASP.NET MVC website.

Tags: , , , , ,

Related posts


No Comments Reply

Leave a Reply

© 2018 Meza Information Technology | Design by Vlad Carp

Back to top