Copied to clipboard

Flag this post as spam?

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


  • Aaron 14 posts 34 karma points
    May 02, 2012 @ 23:52
    Aaron
    0

    Creating new Item in a Task (umbraco 5 + FluentAPI)

    Hey I am trying to create a new item so I can file news away automatically here is the code I have.

    using System;
    using System.Collections.Generic;
    using Umbraco.Hive;
    using Umbraco.Framework.Tasks;
    using Umbraco.Framework.Context;
    using Umbraco.Framework;
    using Umbraco.Cms.Web.Context;
    using Umbraco.Cms.Web.Tasks;
    using Umbraco.Cms.Web.Model;
    using Umbraco.Cms.Web;
    
    
    namespace Umbraco.Gravypower.Whirrakee.Kernel.Tasks
    {
        [Task("25876d45-b834-48be-a2f5-5f5f024a458b", TaskTriggers.Hive.Revisions.PostAddOrUpdate, ContinueOnFailure = false)]
        public class FileNews : AbstractTask
        {
            private IUmbracoApplicationContext _appContext;
    
            public FileNews(IFrameworkContext context, IUmbracoApplicationContext appContext)
                : base(context)
            {
                _appContext = appContext;
            }
            public override void Execute(TaskExecutionContext Context)
            {
                var cms = _appContext.Hive.Cms();
    
                //gets the item that has triggered this task
                var currentItemID = ((HiveRevisionPostActionEventArgs)Context.EventArgs.CallerEventArgs).Entity.Item.Id;
                var currentItem = cms.Content.GetById(currentItemID);
                if(currentItem != null)
                {
                    if (currentItem.ContentType.Alias == "news")
                    {
                        //grab the new folder
                        var newsFolder = cms.Content.GetById(Constants.Content.NewsFolder);
    
                        //gets the date
                        var date = currentItem.Attributes["date"].DynamicValue;
                        var year = (int)date.Year;
                        var yearFolder = newsFolder.Children().SingleOrDefault<Content>("NodeTypeAlias == @0", year);
    
                        if (yearFolder == null)
                        {
                            var yearFolderName = year.ToString();
                            var hiveManager = (IHiveManager)cms;
                            var contentBuilder = hiveManager.Cms().NewRevision(yearFolderName, yearFolderName, "newsFolder")
                                .SetParent(newsFolder.Id)
                                .Publish()
                                .Commit();
    
                            yearFolder = contentBuilder.Content;
                        }
                    }
                }
            }
        }
    }
    

    This however does not want to work and I cant work out what i am doing wrong can anyone help?

     

    Thanks

     

    Aaron

  • Aaron 14 posts 34 karma points
    May 07, 2012 @ 14:09
Please Sign in or register to post replies

Write your reply to:

Draft