Posted by jdemeza81 on
Leave a commentPreviously 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.
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.
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:
Your ‘Cache Storage Management’ settings should now look like this:
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:
Your ‘Magento Core API’ settings should now look like this:
Begin by creating an ‘ASP.NET MVC 4 Web Application (C#)’ in Visual Studio. Then choose either the ‘Basic’ or ‘Internet Application’ template.
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 1 | http://<site_url>/api/soap/?wsdl=1 |
Version 2 | http://<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’.
Confirm that the reference exists by expanding ‘Service References’ in the project.
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.
Add a new controller called ‘CustomerController’ and include the following namespace declaration. The full source of the controller is available on Github.
1 | using MagentoCustomers.local.magento.v2; |
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.
1 2 3 4 | using (var client = new Mage_Api_Model_Server_Wsi_HandlerPortTypeClient()) { var sessionId = client.login(username, password); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | var filters = !String.IsNullOrEmpty(surname) ? new[] { new[] { new complexFilter { key = "lastname", value = new associativeEntity {key = "eq", value = surname} } } } : null; var list = client.mezaitCustomerAddressList(sessionId, filters, pageNum, pageSize); var count = client.mezaitCustomerAddressCount(sessionId, filters); |
Notice we pass in our API key (sessionId) to each call. Once we have finished our work we end our current session.
1 | client.endSession(sessionId); |
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: api, asp.net, c#, magento, MVC, soap
With smart phone and tablet based web browsers controlling a significant share of the market it is becoming increasingly important […]
Read moreI already identified some of the shortcomings of the Magento SOAP API in my previous article including a lack of […]
Read moreI was recently tasked with integrating a Magento eCommerce website and a Windows based point of sales (POS) system. The […]
Read more© 2018 Meza Information Technology | Design by Vlad Carp