.NET examples

In this article:


    To access the shop API from a .NET environment, a proxy client must be loaded. The proxy can be autogenerated by Visual Studio by following this guide. 

     

    • Right-click on References in your project and select "Add Service Reference...":

       
    • Enter the URL of the WSDL file in the "Address" field and click the "Go" button. The URL is:
      https://api.hostedshop.io/service.wsdl

    • You will now see the class "WebServicePort" in the left column and all available methods for the API on the right side:

    • Under "Namespace" you now need to give the reference a name. In this case, we will call it "WebService". Click OK.
    • Now open the "App.Config" file in your project in Visual Studio and find the binding called "WebServiceBinding". Here you need to add the attribute allowCookies="true" to allow the connection to the api to be saved in a session.
    • Use the following example to print a list of products and their variants for a solution in the .NET console. In the example, our new service reference is under the name "ConsoleApplication1.WebService". You can use a specific API user to log in, but if you don't have one, you can use the solution's general login information.

       
    using System; using ConsoleApplication1.WebService; /* Vores projekt hedder "ConsoleApplication1", men du skal her bruge dit eget default namespace */ class Program { static void Main(string[] args) { /* Skab et nyt proxy-objekt */ WebService Client = new WebServicePortClient(); /* Opret forbindelse til en løsning */ Client.Solution_Connect("brugernavn", "password"); /* Sæt sprog for løsningen */ Client.Solution_SetLanguage("DK"); /* Sæt ønskede felter for Produkt-objektet */ Client.Product_SetFields("Id,Title,Variants"); /* Sæt ønskede felter for Produktvariant-objektet */ Client.Product_SetVariantFields("Id"); /* Hent Alle Produkter */ Product[] result = Client.Product_GetAll(); /* Loop over Produkter */ foreach (Product product in result) { /* Udskriv produktets Title */ Console.WriteLine("Produkt: " + product.Title); if (product.Variants.Length > 0) { Console.Write("Varianter:"); /* Loop over produktets varianter */ foreach (ProductVariant variant in product.Variants) { /* Hent variantens TypeValues */ ProductVariantTypeValue[] variantTypeValues = Client.Product_GetVariantTypeValues(variant.Id); /* Loop over variantens TypeValues og udskriv deres Title */ foreach (var variantTypeValue in Client.Product_GetVariantTypeValues(variant.Id)) { Console.Write(" " + variantTypeValue.Title); } Console.WriteLine(""); } } } } }

     

    As seen above, the proxy itself handles the wrapping of input and output arguments. The proxy is generated from https://api.hostedshop.io/service.wsdl. Please see section 2 or https://api.hostedshop.io/doc/ for a detailed documentation of the different method calls available, as they are the same for PHP and .NET.