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 28, 2014 @ 12:02
    Jason Espin
    0

    Scheduling pages in Umbraco

    Hi all,

    I'm currently running an experiment with Umbraco to try and use the code behind of a .aspx page I have created in Visual Studio to run a scheduled import of content into my Umbraco installation from an external web service. The following is the code within my code behind (there is nothing on the front part of the .aspx page, this is blank as it should not be accessed as a normal page).

    My question is, where do I put this within my Umbraco installation and how to I schedule the page to be called every hour or so ensuring that the code behind within the file is run. I also need to know where to move my web service reference. Do I edit the web.config file of my Umbraco installation and include this there?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using WebApplication2.WeatherService;
    
    namespace WebApplication2
    {
      public partial class WebForm1 : System.Web.UI.Page
      {
        protected void Page_Load()
        {
          WeatherSoapClient svc = null;
          bool success = false;
          try
          {
            svc = new WeatherSoapClient("WeatherSoap12");
            var request = 04010;
            var result = svc.GetCityForecastByZIP(request);
            //Get the ContentService which interfaces for content creation/modification    
            int rootId = 1054;
            IContentService cs = ApplicationContext.Current.Services.ContentService;
            int total = result.ForecastResult.Length;  
            foreach (var forecast in result.ForecastResult)
            {
              var weather = cs.CreateContent("Weather Forecast " + forecast.Date, rootId, "weather"); //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
              weather.SetValue("date", forecast.Date);
              weather.SetValue("description", forcast.Description);
    
              //Save and publish the content
              cs.SaveAndPublishWithStatus(weather);
            }
    
            svc.Close();
            success = true;
          }
          finally
          {
            if (!success && svc != null)
            {
              svc.Abort();
            }
          }
        }
      }
    }
    

    Any help would be greatly appreciated.

  • Daniel Harris 33 posts 94 karma points
    Apr 30, 2014 @ 16:44
    Daniel Harris
    100

    Look in the root of your Umbraco install, then go into the config folder and edit umbracoSettings.config

    You need to add it as a scheduled task in the following section:

    <scheduledTasks>
        <!-- add tasks that should be called with an interval (seconds) -->
        <task log="true" alias="test60" interval="3600" url="http://localhost/umbraco/importerpage.aspx"/>
      </scheduledTasks>
    

    Use 3,600 seconds to run it every hour

    This used to have to be an absolute URL in older versions of Umbraco, but this may have changed in version 7 to allow relative locations. Put your page somewhere that a regular user can't access, such as in the /umbraco folder as in the example above.

Please Sign in or register to post replies

Write your reply to:

Draft