Copied to clipboard

Flag this post as spam?

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


  • Paul de Quant 394 posts 1477 karma points
    Jul 28, 2017 @ 12:41
    Paul de Quant
    0

    Automatically Inserting a Task into ScheduledTasks in UmbracoSettings.config

    Hello,

    I'm creating an Umbraco Package and wondered if it was possible to add a new task in the ScheduledTasks section below.

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

    Would this be done in Package Actions and if so how would the code look?

    Thanks

    Paul

  • Paul de Quant 394 posts 1477 karma points
    Jul 28, 2017 @ 13:53
    Paul de Quant
    100

    Found the solution.

    I did have to create a package action. Wasn't easy to find as the package documentation link was broken in the backoffice.

  • Tobias Klika 101 posts 568 karma points c-trib
    Jul 31, 2017 @ 08:59
    Tobias Klika
    0

    As you did ask for the code and marked your own answer as the solution, you could at least have posted the code on how to create a package action ;-)

    Otherwise this topic won't have any use for others searching for a solution.

  • Paul de Quant 394 posts 1477 karma points
    Jul 31, 2017 @ 09:01
    Paul de Quant
    0

    That would be helpful wouldn't it.

            public static bool Install()
        {
            try
            {
    
                bool result = false;
    
                var document = new XmlDocument();
                document.PreserveWhitespace = true;
    
                document.Load(HttpContext.Current.Server.MapPath(FULL_PATH));
    
                bool modified = false;
    
                var rootNode = document.SelectSingleNode("/settings/scheduledTasks");
    
                bool insertNode = true;
    
                //<task log="true" alias="ContentReviewReminders" interval="60" url="http://localhost:17479/backoffice/RemindersApi/ContentReviewReminders/getDetailsForReviewEmail" />
    
                if (rootNode.HasChildNodes)
                {
                    // Look for existing nodeType nodes
                    var oldNode = rootNode.SelectSingleNode(string.Format("task[@name = '{0}']", "ContentReviewReminders"));
    
                    if (oldNode != null) oldNode.ParentNode.RemoveChild(oldNode);
    
                    var node = rootNode.SelectSingleNode(string.Format("task[@name = '{0}']", "ContentReviewReminders"));
    
                    // If name already exists 
                    if (node != null)
                    {
                        // Cancel insert node operation
                        insertNode = false;
                    }
                }
    
                // Check for insert flag
                if (insertNode)
                {
                    string domain = HttpContext.Current.Request.IsSecureConnection ? "https://" + HttpContext.Current.Request.Url.Host + (HttpContext.Current.Request.Url.Port) : "http://" + HttpContext.Current.Request.Url.Host + (HttpContext.Current.Request.Url.Port > 0 ? (":" + HttpContext.Current.Request.Url.Port) : "");
    
                    string domainAndPath = domain + "/backoffice/RemindersApi/ContentReviewReminders/getDetailsForReviewEmail";
    
                    // Create new node with attributes
                    var newNode = document.CreateElement("task");
                    newNode.Attributes.Append(XmlHelper.AddAttribute(document, "log", "true"));
                    newNode.Attributes.Append(XmlHelper.AddAttribute(document, "alias", "ContentReviewReminders"));
                    newNode.Attributes.Append(XmlHelper.AddAttribute(document, "interval", "60"));
                    newNode.Attributes.Append(XmlHelper.AddAttribute(document, "url", domainAndPath));
    
                    // Append new node at the start of root node
                    rootNode.PrependChild(newNode);
    
                    // Mark document modified
                    modified = true;
                }
    
                // Check for modified document
                if (modified)
                {
                    // Save the Rewrite config file with the new rewerite rule
                    document.Save(HttpContext.Current.Server.MapPath(FULL_PATH));
    
                    // No errors so the result is true
                    result = true;
                }
    
                return result;
            }
            catch
            {
                return false;
            }
    
    
        }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies