5

I would like to know (a sample code if it is not too much to ask for) how to talk to Magento 2's SOAP API from C# using Webservice (not service reference). With Magento 1.x versions I used something like

static MagentoWebService.MagentoService MyService = new MagentoWebService.MagentoService();

static string Login = MyService.login(Config.Login, Config.Password);

where as login being,

    public string login(string username, string apiKey) 
    {
        object[] results = this.Invoke("login", new object[] {
                    username,
                    apiKey});
        return ((string)(results[0]));
    }

How do I do the same/similar one with Magento 2. I have seen few example given in PHP. However, I am struggling to convert that in to C# or understand properly. Could someone please help with this.

So far in my new code I tried this:

catalogProductRepositoryV1Service productservice = new catalogProductRepositoryV1Service();
        CatalogProductRepositoryV1GetResponse response = new CatalogProductRepositoryV1GetResponse();
        CatalogProductRepositoryV1GetRequest request = new CatalogProductRepositoryV1GetRequest();
        request.sku = "WJ01";
        response = productservice.catalogProductRepositoryV1Get(request);
        Console.WriteLine(response.result.id.ToString());

But it wont work, since I haven't logged in to the SOAP service yet.

1

I was able to make requests to Magento 2 web API using the following example in 2014 (before 2.0 release), but it should work now as well.

To run it, follow the steps:

  1. Open Visual Studio C# 2010 Express
  2. Create new console application project
  3. In the project tree click on the project root to view context menu and select "Add Service Reference"
  4. Set address to http://<magento.host>/soap/default?wsdl&services=customerCustomerAccountServiceV1 namespace to MagentoService
  5. Add example below to your project and update Access token field with actual value
  6. Run the program, console will be opened and simple attribute values are displayed

C# console application example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.MagentoService;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace ConsoleApplication1
{
    class Program
    {
        const string accessToken = "9b8cpqlvrberc51o02nccmg82yyechxe";

        static void Main(string[] args)
        {
            customerCustomerAccountServiceV1PortTypeClient customerApi = new customerCustomerAccountServiceV1PortTypeClient();
            CustomerCustomerAccountServiceV1GetCustomerRequest request = new CustomerCustomerAccountServiceV1GetCustomerRequest();
            request.customerId = 1;
            CustomerCustomerAccountServiceV1GetCustomerResponse response;
            try
            {
                /** Set authorization headers and sent a request */
                using (OperationContextScope scope = new OperationContextScope(customerApi.InnerChannel))
                {
                    var httpRequestProperty = new HttpRequestMessageProperty();
                    httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Bearer " + accessToken;
                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

                    response = customerApi.customerCustomerAccountServiceV1GetCustomer(request);
                }

                CustomerV1DataCustomer customerData = response.result;
                Console.Out.WriteLine("Customer email is: {0}.", customerData.email);
                Console.Out.WriteLine("Custom attributes:", customerData.email);
                foreach (FrameworkDataEavAttributeValue attribute in customerData.customAttributes)
                {
                    /** Cast object of generic type to its real type */
                    System.Xml.XmlNode[] value = (System.Xml.XmlNode[])attribute.value;
                    Console.WriteLine("{0}: {1}", attribute.attributeCode, value[0].Value);
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.ToString());
            }
            Console.In.Read();

        }
    }
}

This example uses Service reference (not Webservice), but still might be helpful to understand how to pass authentication headers.

  • Thanks for your time Alex :) I have it working using service reference. But for some reason (serialization error, i logged before) i would like to check with web reference too. With web reference there is no option i can see of to provide access token or creating http header. – Vic85 Apr 12 '16 at 6:01
0

If you want to add service references, here is a list of them broken down by magento type. You'll have to disable soap security for them to work with c#.

https://yourwebsite.com/soap/default?wsdl&services=storeStoreRepositoryV1,storeGroupRepositoryV1,storeWebsiteRepositoryV1,storeStoreConfigManagerV1

https://yourwebsite.com/soap/default?wsdl&services=directoryCurrencyInformationAcquirerV1,directoryCountryInformationAcquirerV1

https://yourwebsite.com/soap/default?wsdl&services=backendModuleServiceV1

https://yourwebsite.com/soap/default?wsdl&services=eavAttributeSetRepositoryV1,eavAttributeSetManagementV1

https://yourwebsite.com/soap/default?wsdl&services=searchV1

https://yourwebsite.com/soap/default?wsdl&services=customerGroupRepositoryV1,customerGroupManagementV1,customerCustomerGroupConfigV1,customerCustomerMetadataV1,customerAddressMetadataV1,customerCustomerRepositoryV1,customerAccountManagementV1,customerAddressRepositoryV1

https://yourwebsite.com/soap/default?wsdl&services=cmsPageRepositoryV1

https://yourwebsite.com/soap/default?wsdl&services=cmsBlockRepositoryV1

https://yourwebsite.com/soap/default?wsdl&services=catalogProductRepositoryV1,catalogProductAttributeTypesListV1,catalogProductAttributeRepositoryV1,catalogCategoryAttributeRepositoryV1,catalogCategoryAttributeOptionManagementV1,catalogProductTypeListV1,catalogAttributeSetRepositoryV1,catalogAttributeSetManagementV1,catalogProductAttributeManagementV1,catalogProductAttributeGroupRepositoryV1,catalogProductAttributeOptionManagementV1,catalogProductMediaAttributeManagementV1,catalogProductAttributeMediaGalleryManagementV1,catalogProductTierPriceManagementV1,catalogTierPriceStorageV1,catalogBasePriceStorageV1,catalogCostStorageV1,catalogSpecialPriceStorageV1,catalogCategoryRepositoryV1,catalogCategoryManagementV1,catalogCategoryListV1,catalogProductCustomOptionTypeListV1,catalogProductCustomOptionRepositoryV1,catalogProductLinkTypeListV1,catalogProductLinkManagementV1,catalogProductLinkRepositoryV1,catalogCategoryLinkManagementV1,catalogCategoryLinkRepositoryV1,catalogProductWebsiteLinkRepositoryV1,catalogProductRenderListV1,catalogInventoryStockRegistryV1

https://yourwebsite.com/soap/default?wsdl&services=quoteCartRepositoryV1,quoteCartManagementV1,quoteGuestCartRepositoryV1,quoteGuestCartManagementV1,quoteShippingMethodManagementV1,quoteShipmentEstimationV1,quoteGuestShippingMethodManagementV1,quoteGuestShipmentEstimationV1,quoteCartItemRepositoryV1,quoteGuestCartItemRepositoryV1,quotePaymentMethodManagementV1,quoteGuestPaymentMethodManagementV1,quoteBillingAddressManagementV1,quoteGuestBillingAddressManagementV1,quoteCouponManagementV1,quoteGuestCouponManagementV1,quoteCartTotalRepositoryV1,quoteGuestCartTotalManagementV1,quoteGuestCartTotalRepositoryV1,quoteCartTotalManagementV1

https://yourwebsite.com/soap/default?wsdl&services=salesOrderRepositoryV1,salesOrderManagementV1,salesOrderAddressRepositoryV1,salesOrderItemRepositoryV1,salesInvoiceRepositoryV1,salesInvoiceManagementV1,salesInvoiceCommentRepositoryV1,salesRefundInvoiceV1,salesCreditmemoManagementV1,salesCreditmemoRepositoryV1,salesCreditmemoCommentRepositoryV1,salesRefundOrderV1,salesShipmentRepositoryV1,salesShipmentManagementV1,salesShipmentCommentRepositoryV1,salesShipmentTrackRepositoryV1,salesShipOrderV1,salesTransactionRepositoryV1,salesInvoiceOrderV1

https://yourwebsite.com/soap/default?wsdl&services=checkoutGuestShippingInformationManagementV1,checkoutShippingInformationManagementV1,checkoutTotalsInformationManagementV1,checkoutGuestTotalsInformationManagementV1,checkoutGuestPaymentInformationManagementV1,checkoutPaymentInformationManagementV1

https://yourwebsite.com/soap/default?wsdl&services=configurableProductLinkManagementV1,configurableProductConfigurableProductManagementV1,configurableProductOptionRepositoryV1


https://yourwebsite.com/soap/default?wsdl&services=integrationAdminTokenServiceV1,integrationCustomerTokenServiceV1

https://yourwebsite.com/soap/default?wsdl&services=asynchronousOperationsBulkStatusV1,asynchronousOperationsOperationRepositoryV1

https://yourwebsite.com/soap/default?wsdl&services=downloadableLinkRepositoryV1,downloadableSampleRepositoryV1

https://yourwebsite.com/soap/default?wsdl&services=bundleProductLinkManagementV1,bundleProductOptionRepositoryV1,bundleProductOptionTypeListV1,bundleProductOptionManagementV1

https://yourwebsite.com/soap/default?wsdl&services=giftMessageCartRepositoryV1,giftMessageItemRepositoryV1,giftMessageGuestCartRepositoryV1,giftMessageGuestItemRepositoryV1

https://yourwebsite.com/soap/default?wsdl&services=analyticsLinkProviderV1

https://yourwebsite.com/soap/default?wsdl&services=salesRuleRuleRepositoryV1,salesRuleCouponRepositoryV1,salesRuleCouponManagementV1

https://yourwebsite.com/soap/default?wsdl&services=checkoutAgreementsCheckoutAgreementsRepositoryV1,checkoutAgreementsCheckoutAgreementsListV1

https://yourwebsite.com/soap/default?wsdl&services=taxTaxRateRepositoryV1,taxTaxRuleRepositoryV1,taxTaxClassRepositoryV1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.