Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jason Espin 368 posts 1335 karma points
    Apr 24, 2014 @ 13:16
    Jason Espin
    0

    Scheduled import of content from 3rd party web service with authentication

    Hi all,

    We are currently using Umbraco for the first time and would like some guidance with regards to the following:

    We have a core software product that uses a set of web services allowing our clients to create content and packages that we would then like to pull into Umbraco and display on the front end. So, for example, our customer creates a product along with related information such as images, pricing and description etc. The web services of our product would then be called by Umbraco each evening to create/replace/update pages/content based upon the products that are within the system.

    The request sent to our system from Umbraco would have to contain a specific key which identifies that the customer is registered with us. I don't know if this behaviour would be a problem.

    Is there an API for this or even better and existing Umbraco plugin that would support this behaviour? Preferably the latest version of Umbraco would be best.

    Any help would be greatly appreciated.

  • Daniel Harris 33 posts 94 karma points
    Apr 24, 2014 @ 17:52
    Daniel Harris
    0

    Hi Jason,

    There are a few ways you could do this, but I can't think of an existing package that would do it as the information you are pulling in will be specific to your needs and if storing this information into Umbraco, you will be storing it into your own custom document types.

    It should be a case of triggering an automated task that will parse the data from your web service, and then creates/updates the relevant nodes in Umbraco.

    You can pull down the Umbraco Core via NuGet, which should enable you to create nodes programmatically. How you grab and parse the data is really up to you and what you think is best.

    You can find the reference for creating content here: http://our.umbraco.org/documentation/reference/Management-v6/Services/ContentService

    I've not used the WebApi functionality of Umbraco yet, but I would assume this allows you to create data as well as read it, so you could have the existing systems push the data in via this if that would be easier:

    http://our.umbraco.org/documentation/reference/WebApi/

    Deployment

    One way could be to have it deployed as an Umbraco Task, which would run in the context of the application.

    You could alternatively create a console application and schedule it to run, or if running on Azure Websites as an Azure Webjob. Likewise it could be a Windows Service if it needed to be.

    Not sure how useful this info will be to you without you looking into things in a bit more detail and experimenting, hopefully it's helped. Without knowing in detail what sort of data you are pulling in etc it would be difficult to offer anything more concrete.

    I can try and put some sort of example together but it may be over the weekend, if I do I will post it to my blog and here.

  • Daniel Harris 33 posts 94 karma points
    Apr 24, 2014 @ 23:08
    Daniel Harris
    100

    Here is some untested code that should allow you to add content. Below is in the context of this running in a page within Umbraco, which you can then configure to run as a scheduled task. This is just one way to schedule the task, and not necessarily the best choice for your scenario.

    This assumes I have created a document type (Product) with some specific properties. The code for retrieving and parsing the data from the 3rd parties would be regular .NET code, you just need to then push the data into Umbraco.

    You could remove them all and re-add them, or put in some logic to Update/Insert as appropriate.

    I think there are many ways to do this, and it will depend on your existing systems etc.

    //Get the ContentService which interfaces for content creation/modification    
    int rootId = 1054;
    IContentService cs = ApplicationContext.Current.Services.ContentService;
    
    //Create the new content node we will be saving
    var product = cs.CreateContent("Great New Product", rootId, "product"); //Node name, Parent Nodes Id, Alias of Document Type
    
    //Set the properties using the SetValue method, passing in the alias of the property and the value to assign to it
    product.SetValue("quantityPerUnit", productInfo["QuantityPerUnit"]);
    product.SetValue("unitsInStock", productInfo["UnitsInStock"]);
    
    //Save and publish the content
    cs.SaveAndPublishWithStatus(product);
    

    Also add the following using statements

    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    

    It would be interesting to know if there are already solutions out there, but so far I haven't seen any. We pull in data from our internal systems regularly in an older install of Umbraco, but we pull them into UCommerce in a different way.

  • Jason Espin 368 posts 1335 karma points
    Apr 25, 2014 @ 11:46
    Jason Espin
    0

    Hi Daniel,

    Thanks for the help so far. You seem to have jumped several steps though. I've managed to put together a simple console application in C# that uses a service reference to make a web service call. If I understand correctly, if I plug this into Umbraco I should be able to call the web service and use some of the code you have provided above to store the response from the web service as content in one of my pages.

    The problem is, what is the rootID referring to? Also, where do I put my service reference / app config / general code?

    Sorry for all of the questions, I'm learning .Net at the same time as learning Umbraco so this is all very new to me considering I come from a Javascript/Front-end development background.

  • Daniel Harris 33 posts 94 karma points
    Apr 25, 2014 @ 12:24
    Daniel Harris
    0

    Hi Jason,

    No problem. I'll try and answer your questions as best I can.

    The root ID variable (Which I didn't explain) is the ID of the root node in the Umbraco install. I'm just using it in this example to place the created node under the root of the site.

    I can see I missed an important line in the code above. I've edited the response so that it now has the following additional line, where you can see the root ID being used to determine which node will be the parent of my new node:

    //Create the new content node we will be saving
    var product = cs.CreateContent("Great New Product", rootId, "product"); //Node name, Parent Nodes Id, Alias of Document Type
    

    You are right to say that once you have the response from your call to the web service, you can then use similar code to loop through the results and insert/update the nodes (content) in Umbraco.

    You've said you are doing this from a console application, but executing the code from within the context of the web application (Umbraco) would make things more simple.

    You can still use the same code, but it's a bit more involved when it comes to wiring things up so you can execute this code against your Umbraco instance.

    I would personally (without knowing in full the size of this project) most likely consider exposing a REST endpoint via WebApi from within your Umbraco site, which when running in the context of Umbraco it makes it a lot simpler to execute the above code.

    You could then have your console application make the call to pull down the data from the original web service, then make a request to your endpoint via another web service call into Umbraco.

    Alternatively you could drop the console application completely, and just have (As an example) as ashx handler that runs the code (currently in your console app) to grab data from the web service and update the Umbraco content as needed.

    In any scenario you will need to assess what security measures you need to put in place to stop unauthorised use of the content update functionality.

    There is some Web API documentation available here:

    http://our.umbraco.org/documentation/Reference/WebApi/

    And you can also take a look at a sample showing what's needed to run this sort of code from outside of Umbraco:

    https://github.com/sitereactor/umbraco-console-example

  • Jason Espin 368 posts 1335 karma points
    Apr 25, 2014 @ 13:04
    Jason Espin
    0

    Okay. It's still all a bit unclear to me. Apologies that I require a lot of hand-holding for this first implementation I'm just very new to all of this.

  • Daniel Harris 33 posts 94 karma points
    Apr 25, 2014 @ 14:08
    Daniel Harris
    0

    Is there a specific part you are stuck on at the moment?

    If I was you I would forget the console app for now, and try executing code similar to the above in the Page_Load event of an aspx page, or create an ASHX handler and do the same. Worry about scheduling it and the security etc once you have the base code working.

    This would help you get familiar with working with the Umbraco API in a simple way. As I say executing it from a console app will ultimately be a little more complicated, so best to start with a simple test.

    If I can upload an example for you I will do.

    Are you working with Umbraco in Visual Studio on your local machine at the moment?

  • Jason Espin 368 posts 1335 karma points
    Apr 25, 2014 @ 14:27
    Jason Espin
    0

    Hi Daniel,

    Yes, I'm currently using Visual Studio 2013 on my machine. I've just been watching the Umbraco.tv videos on the Content API and it suggest on there that you can use a surface controller to create Umbraco content. If I plugged my console application code into the controller would this work? The main part i'm confused about is where I move my service reference in Visual Studio to in terms of Umbraco so that I can still use this simple methodology of accessing the web service.

    Below is the code for my console application that way you'll be able to see what I mean:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using ConsoleApplication1.Service1Reference;
    
    namespace ConsoleApplication1
    {
      class Program
    {
      static void Main(string[] args)
    {
      Console.Write("Enter ZipCode: ");
      var line = Console.ReadLine();
      if (line == null)
      {
        return;
      }
    
      WeatherSoapClient svc = null;
      bool success = false;
      try
      {
        svc = new WeatherSoapClient("WeatherSoap12");
    
        var request = line;
        var result = svc.GetCityForecastByZIP(request);
        Console.Clear();
        Console.WriteLine("7 Day Forecast for " + result.City + "," + result.State);
        Console.WriteLine("------------------------------------------------------------------");
        int count = 0;
        int total = result.ForecastResult.Length;
        foreach (var rs in result.ForecastResult){
          if (count > 0)
          {
            Console.WriteLine("************************");
          }
          Console.WriteLine("Date:" + rs.Date);
          Console.WriteLine("Forecast:" + rs.Desciption);
          Console.WriteLine("Temperatures:");
          Console.WriteLine("Morning low - " + rs.Temperatures.MorningLow);
          Console.WriteLine("Daytime high - " + rs.Temperatures.DaytimeHigh);
          count++;
        }
        Console.WriteLine("------------------------------------------------------------------");
        Console.Write("ENTER to continue:");
        Console.ReadLine();
    
        svc.Close();
        success = true;
      }
      finally
      {
        if (!success && svc != null)
        {
          svc.Abort();
        }
      }
    }
    }
    }
    

    Regards

    Jason

  • Jason Espin 368 posts 1335 karma points
    Apr 25, 2014 @ 17:46
    Jason Espin
    0

    Do I just create a blank .aspx page and use only the code behind section to call the web service in the same way I am doing in my console application but instead of outputting to the console, the values would be used to populate Umbraco property values. I would then store my .aspx page somewhere within the Umbraco directories and update the umbraco web.config file with the service reference for my project. After doing this, I would schedule a task point to the URL of my .aspx page using this:

    http://our.umbraco.org/wiki/install-and-setup/scheduled-tasks

    That would then technically run the web service, populate the content and it will then be present in my Umbraco installation. Theoretically of course. With this in mind, are the methods for calling the web service using a .aspx page fundamentally the same as my console application as it took me ages to find that example of how to call a web service using service references.

  • Jason Espin 368 posts 1335 karma points
    Apr 28, 2014 @ 13:49
    Jason Espin
    0

    I managed to get this working by the way using a combination of the code Daniel provided above and my own trial and error. Thanks a bunch for all of your help Daniel.

Please Sign in or register to post replies

Write your reply to:

Draft