Copied to clipboard

Flag this post as spam?

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


  • keilo 568 posts 1023 karma points
    Aug 30, 2014 @ 21:07
    keilo
    0

    How to create a sub-node on page creation

    Thank you for this very cool package. It is a good starting point to learn the new event system and make a real life use-case of it.

    I am trying to create a new Archive node, whenever a new Category (newsOverview) created. I have multiple categories under a main root node and would like to create/keep a category specific Archive node automatically - whenever the new category is created.

    Saw your post on http://our.umbraco.org/forum/developers/extending-umbraco/38477-ContentService-Created-Event-Entity-doesnt-have-ID?p=0 which suggests to use

    var isNew = entity.IsNewEntity();

    Tried to edit the .cs code incporporating the above without sucesss..

    Would you be able to advise how to go about implementing this? Im using 7.1.6

    cheers

  • Sören Deger 733 posts 2844 karma points c-trib
    Sep 01, 2014 @ 08:17
    Sören Deger
    1

    Hi Keilo,

    edit the saved event:

    void ContentService_Saved(IContentService sender, SaveEventArgs e)
            {
                foreach (IContent node in e.SavedEntities)
                {
                    if (node.ContentType.Alias == "umbNewsOverview")
                    {
                        if(node.IsNewEntity())
                        {
                            IContent newNewsArchive = sender.CreateContent(node.Name + "_archive", node.Parent(), "NewsArchive"); 
                            sender.SaveAndPublishWithStatus(newNewsArchive);
                        }
                    } else if(node.ContentType.Alias == newsItemAlias)
                    {
    ... 

    Maybe this should be work for you.

     

    Sören

  • keilo 568 posts 1023 karma points
    Sep 01, 2014 @ 11:41
    keilo
    0

    Hi Soren

    Tried adding the code you suggested. It throws the following error:

     

    Compiler Error Message: CS1061: 'Umbraco.Core.Models.IContent' does not contain a definition for 'parent' and no extension method 'parent' accepting a first argument of type 'Umbraco.Core.Models.IContent' could be found (are you missing a using directive or an assembly reference?)
    

    This is for the line:

      IContent newNewsArchive = sender.CreateContent(node.Name + "_archive", node.parent(), "NewsArchive"); 
    

    Any suggestion on how I can fix this?

  • Sören Deger 733 posts 2844 karma points c-trib
    Sep 01, 2014 @ 11:50
    Sören Deger
    100

    Hi Keilo,

    write Parent() with an uppercase letter:

    IContent newNewsArchive = sender.CreateContent(node.Name + "_archive", node.Parent(), "NewsArchive");  

    This should be work.

     

    Sören

  • keilo 568 posts 1023 karma points
    Sep 01, 2014 @ 11:52
    keilo
    0

    Sorry, didnt notice the typo, it should read

      IContent newNewsArchive = sender.CreateContent(node.Name+"_archive", node.Parent(),"NewsArchive");

    If I would like to create the Archive node inside the Category, would it make any sense?

    Ideally I would like to create the Archive node insider the Category, and keep it as the top-most node. But with the sorting algorithm the Archive node will always keep changing location. Would there be a way to exclude it from the sorting and ensure its always the first node under the Category name?

    NewsCategory1

      Archive

      article2

      article1

      ...

     

     

  • Sören Deger 733 posts 2844 karma points c-trib
    Sep 01, 2014 @ 12:03
    Sören Deger
    1

    Of course. You can exclude this in declaration of newsList. Maybe this can solve your problem.

    IEnumerable<IContent> newsList = settings.Children().Where(x => x.ContentType.Alias!="NewsArchive");


    Sören

  • Sören Deger 733 posts 2844 karma points c-trib
    Sep 01, 2014 @ 12:09
    Sören Deger
    1

    I post the solution to set the archive node on first position later, maybe this evening. But its very simple.

  • keilo 568 posts 1023 karma points
    Sep 01, 2014 @ 12:33
    keilo
    0

    Followed your advise and changed the newsList declaration to

    IEnumerable<IContent> newsList = settings.Children().Where(x => x.ContentType.Alias!="NewsArchive");

    and changed the code to create the Archive node as a Child (node.Id) of the category created; (off-topic, now that newarchive created automatically, one can go ahead and set the Archive node to the auto-created one withing the code perhaps? not sure if its a good or bad idea though..)

     

      IContent newNewsArchive = sender.CreateContent(node.Name+"_archive", node.Id,"NewsArchive");

    When creating a new category, it creates the Archive node under successfully. All looks great - However when I try to create a new article under the newly created category, i get this error:

    Specified cast is not valid.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.InvalidCastException: Specified cast is not valid. 
    
    Stack Trace: 
    at EventhandlingCocktail.App_Code.uNewsManager.ContentService_Saving(IContentService sender, SaveEventArgs`1 e) in d:\WWWroot\umbraco\App_Code\uNewsManager.cs:line 128
       at Umbraco.Core.Services.ContentService.SaveAndPublishDo(IContent content, Int32 userId, Boolean raiseEvents)
       at Umbraco.Web.Editors.ContentController.PostSave(ContentItemSave contentItem)
       at lambda_method(Closure , Object , Object[] )
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.b__c(Object instance, Object[] methodParameters)
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
       at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
    
    

    When i get this error, i navigate to a different node then back to the new one, try again and get the same error. For some very strange reason, when I refresh the page on another node, navigate back to the new created node and create a new article it worked. Then tried creating another new category node and a newsitem under and it gave the same error...

    So Im not sure what is causing it but definitely strange.

    Would look out for your reply. Cheers!

  • Sören Deger 733 posts 2844 karma points c-trib
    Sep 01, 2014 @ 13:59
    Sören Deger
    1

    Change this line in saved-event

    if (settings.HasProperty("autoSort") && (int)settings.GetValue("autoSort") == 1)

    to 

    if (settings.HasProperty("autoSort") && Convert.ToInt32(settings.GetValue("autoSort")) == 1)

     

    and change this line in saving-event

    if (node.ExpireDate == null && (int)settings.GetValue("autoExpire") == 1)

    to

    if (node.ExpireDate == null && Convert.ToInt32(settings.GetValue("autoExpire")) == 1)

     

    If it works for you I will update the package later and add an option in newsOverview document type to create automatically archive node under newsOverview node.

     

    Sören

  • keilo 568 posts 1023 karma points
    Sep 01, 2014 @ 14:17
    keilo
    0

    Yes, that worked indeed!

    For adding the feature option to create archive node automatically, would be interesting to know your plan (i.e. as child-node, setting archive node auto, not-changeable etc.)

    cheers!

  • keilo 568 posts 1023 karma points
    Sep 01, 2014 @ 14:27
    keilo
    0

    Just a quick update.

    When I tried to delete an existing category created earlier, I go the following error;

     Failed to delete item 1444


    Specified cast is not valid.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.InvalidCastException: Specified cast is not valid. 

    Stack Trace: 

    at EventhandlingCocktail.App_Code.uNewsManager.ContentService_UnPublished(IPublishingStrategy sender, PublishEventArgs`1 e) in d:\WWWroot\umbraco\App_Code\uNewsManager.cs:line 107

       at Umbraco.Core.Events.TypedEventHandler`2.Invoke(TSender sender, TEventArgs e)

       at Umbraco.Core.Services.ContentService.UnPublishDo(IContent content, Boolean omitCacheRefresh, Int32 userId)

       at Umbraco.Core.Services.ContentService.MoveToRecycleBin(IContent content, Int32 userId)

       at Umbraco.Web.Editors.ContentController.DeleteById(Int32 id)

       at lambda_method(Closure , Object , Object[] )

       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)

       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)

       at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)

  • keilo 568 posts 1023 karma points
    Sep 01, 2014 @ 14:44
    keilo
    1

    To answer the issue in the earlier post, if the _Unpublished event updated

    where the (int) castings replaced with Convert.ToInt32(), it solves the error on delete above. (didnt occur to me before delete fires unpublish, only logical though..)

     

  • keilo 568 posts 1023 karma points
    Sep 01, 2014 @ 17:07
    keilo
    0

    I think I hit a bug, when the trying to delete one news category and it throw int casting error - the entire NewsRoot (i.e. all categories and its sub-nodes) were un-published within umbraco. I have fixed the int casting error as per the above post but later noticed the issue...

    As I enabled Auto Archive in few categories, which contains hundreds of article entries (active), it moved *all* of them under the Archive node...

    So my question would be, is there a simple way to move them back to the category root (i.e. out of the archive)? since going through one-by-one will not be practical as there are hundreds of them.

    not sure how to go about solving it. any ideas?

  • Sören Deger 733 posts 2844 karma points c-trib
    Sep 02, 2014 @ 06:16
    Sören Deger
    1

    You can use the ContentService to move them back to the category root.

  • Sören Deger 733 posts 2844 karma points c-trib
    Sep 02, 2014 @ 08:53
    Sören Deger
    1

    Hi Keilo,

    I have update this package to version 1.2. The delete bug should be solved. Now you can create the archive node as first child of your newsOverview node automatically and this archive node is everytime at first position and excluded from news sorting.

    Hope this helps.

     

    Sören

  • keilo 568 posts 1023 karma points
    Sep 02, 2014 @ 10:09
    keilo
    0

    Hi Soren

    Managed to write a razor in template. For reference who came across similar issue, you can use a script similar to this and execute on the archive node (where archive node is inside the category):

    quick question; what does the last two params mean (google for a good reference but couldnt find one yet) in  cs.SaveAndPublishWithStatus(art, 0, false);

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @using System;
    
    @using Umbraco.Core;
    
    @using Umbraco.Core.Events;
    
    @using Umbraco.Core.Models;
    
    @using Umbraco.Core.Publishing;
    
    @using Umbraco.Core.Services;
    
    @{
    
        IContentService cs = ApplicationContext.Current.Services.ContentService;
    
        var moveToID= Model.Content.Parent.Id; // category root to move to
    
        var nodes = cs.GetById(Convert.ToInt32(Model.Content.Id)); // assume current page is the Archive node
    
        foreach (IContent art in  nodes.Children()){
    
            cs.Move(art, Convert.ToInt32(moveToID));
    
            cs.SaveAndPublishWithStatus(art, 0, false);
    
        }   
    
    }
    

     

  • Sören Deger 733 posts 2844 karma points c-trib
    Sep 02, 2014 @ 10:38
    Sören Deger
    1

    You can found the reference here:

    http://our.umbraco.org/documentation/Reference/Management-v6/Services/ContentService ;

    0 is the user id and false is an optional boolean indicating whether or not to raise save events.

     

Please Sign in or register to post replies

Write your reply to:

Draft