Copied to clipboard

Flag this post as spam?

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


  • jonney.corner 13 posts 33 karma points
    May 23, 2013 @ 14:49
    jonney.corner
    0

    Archiving News Items Into an Archive Folder

    Hello all!

    I am looking to archieve a new functionality on my umbraco site, which will run every week or so and check to see if there is any News items which are older than 6 months, if so i would like to have these items automatically moved into a new folder i have created called 'Archive'. I would then like to have any items older than 12 months old in the Archive folder to be automatically deleted.

    My current Content structure is:

    Home
    About
    About Page 1
    Company News
    News Item 1
    News Item 2
    News Item 3
    Archive
    Deleted Items

    Your suggestions will be greatly appreciated

    Thanks.

  • Len Dierickx 150 posts 92 karma points
    May 23, 2013 @ 15:51
    Len Dierickx
    0

    Hi,

    What you mean with automatically isn't entirely clear, the action needs to be triggered.
    You can schedule tasks on the server, or use the scheduled task functionality http://our.umbraco.org/wiki/reference/packaging/package-actions/community-made-package-actions/add-a-scheduled-task or use the umbraco events and run the action when a new news item is published for example. 

    I think that there are several solutions, though the wiki has the an example with umbraco events.http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events/event-examples

    The example requires an intervention form an editor, but change the rule to a date calculation and use the publish event to trigger the move:

    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.web;
     
    namespaceAutoMoveToArchive
    {
       
    /// <summary>
       
    /// Auto move demo
       
    /// </summary>
       
    publicclassAutoMoveToArchive:ApplicationBase
       
    {
           
    /// <summary>
           
    /// Wire up the event handler
           
    /// </summary>
           
    publicAutoMoveToArchive()
           
    {
               
    Document.BeforeSave+=newDocument.SaveEventHandler(Document_BeforeSave);
           
    }

     
           
    /// <summary>
           
    /// Before a documents get saved we wil check if the archived proerty is.
           
    /// When it's set move it to the archive folder.
           
    /// Again it;s demo code, normally you should also check if it's not allready IN the archived foder ;-)
           
    /// </summary>
           
    voidDocument_BeforeSave(Document sender, umbraco.cms.businesslogic.SaveEventArgs e)
           
    {
               
    // Set Arrchive folder id, no excuse to use hard coded values in Umbraco ;-)
               
    int archiveId =1123;
               
    //Log that we are doing something
               
    Log.Add(LogTypes.Custom, sender.Id,"Document After save Raised");
     
               
    //Check if the item is news and must e archived
               
    if(sender.ContentType.Alias=="News"&& sender.getProperty("archived").Value.Equals(1))
               
    {
                   
    //Yes, move to archive
                    sender
    .Move(archiveId);
     
                   
    //Unpublish from current Node
                    umbraco
    .library.UnPublishSingleNode(sender.Id);
                    sender
    .UnPublish();
                   
    //Publish will get called if the user selected Save and publish
               
    }
           
    }
       
    }
    }

     

    To delete items, you could use a XSLT library with the following code:

    ///<summary>

    /// Deletes a node from the tree, removal to trash bin

    /// </summary>

    /// <param name="NodeId">The node id of the page</param>

    /// <returns> Boolena</returns>

    public static string DeleteNode(int NodeId) {

        try

        {

            Document d = new Document(NodeId);

            d.delete();

            return "true";

        }

        catch (Exception e) {

            umbraco.BusinessLogic.Log.Add(LogTypes.Error, -1, e.ToString() + " Nodeid:" + NodeId);

            return "false"; 

        }

     

    }

  • jonney.corner 13 posts 33 karma points
    May 24, 2013 @ 10:10
    jonney.corner
    0

    Thats great thank you soo much,
    Any example of the event handler?

    My experience in coding is not its best haha! 

  • Len Dierickx 150 posts 92 karma points
    May 24, 2013 @ 10:19
    Len Dierickx
    0

    The event handler is in the code above:

    /// <summary>
           
    /// Wire up the event handler
           
    /// </summary>
           
    public AutoMoveToArchive()
           
    {
               
    Document.BeforeSave+=newDocument.SaveEventHandler(Document_BeforeSave);
           
    }

    Before saving the document, Document_BeforeSave will be excuted

    The events are documented on the wiki: 

    http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events/overview-of-all-events

    And there is more info on the api cheat sheet:

    http://our.umbraco.org/wiki/reference/api-cheatsheet

     

    If you show me what you already have, then maybe I can help you out. 

    L.

     

  • jonney.corner 13 posts 33 karma points
    May 24, 2013 @ 10:24
    jonney.corner
    0

    Thats great, thank you!

  • jonney.corner 13 posts 33 karma points
    May 24, 2013 @ 11:07
    jonney.corner
    0

    How do i apply this code to the Site? is it through a macro?

  • jonney.corner 13 posts 33 karma points
    May 24, 2013 @ 15:36
    jonney.corner
    0

    I also need an event handler before i add the code to the site, thats another thing i am unable to do.

  • Len Dierickx 150 posts 92 karma points
    May 24, 2013 @ 16:06
    Len Dierickx
    0

    You can compile the code, or put it in a file in the App_Code folder.
    The event handler is already there. You don't need to do more than that. 

    And all the events are in the document: http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events/event-examples ;

    This isn't going to work out of the box, you will have to do some coding to get this working. 

    Show me what code you already have, and I'll see if I can help you out. 

     

     

     

     

  • jonney.corner 13 posts 33 karma points
    May 28, 2013 @ 11:01
    jonney.corner
    0

    for the Archive main Script code i have saved it as a CSHTML File with the following code:

    using umbraco.BusinessLogic;

    using umbraco.cms.businesslogic.web;

     

    namespaceAutoMoveToArchive

    {

    /// Auto Move Function

        publicclassAutoMoveToArchive:ApplicationBase

        {

            /// Below is link to the event handler

            publicAutoMoveToArchive()

            {

                Document.BeforeSave+=newDocument.SaveEventHandler(Document_BeforeSave);

            }

     

     

            /// Archive Property Check

            voidDocument_BeforeSave(Document sender, umbraco.cms.businesslogic.SaveEventArgs e)

            {

                /// Archive Folder ID

                int archiveId =1138; 

     

                /// Log what happens

                Log.Add(LogTypes.Custom, sender.Id,"Document After save Raised");

     

                /// Checking to See if News item is not Archived

                if(sender.ContentType.Alias=="News"&& sender.getProperty("archived").Value.Equals(1))

                {

                    /// If so Move to Archive Folder by ID

                    sender.Move(archiveId);

     

                    // Unpublish Item

                    umbraco.library.UnPublishSingleNode(sender.Id);

                    sender.UnPublish();

     

                    /// Publish will get called if the user selected Save and publish

                }

            }

        }

    }

     

    and for the event trigger, i have added this into the Umbraco.config file between the <schedule> tag with the following code:

     

            publicAutoMoveToArchive()
           
    {
               
    Document.BeforeSave+=newDocument.SaveEventHandler(Document_BeforeSave);
           
    }
  • Len Dierickx 150 posts 92 karma points
    May 29, 2013 @ 10:44
    Len Dierickx
    0

    Ok, ic, this not how it works. 

    Create a file, in the App_Code folder called "updateArchive.cs"

    Paste the code from the example: http://our.umbraco.org/wiki/reference/packaging/package-actions/community-made-package-actions/add-a-scheduled-task

    Create a Document type called "News" with a property "archived" (boolean) and any other properties that you would need (title, content, etc.).
    Then you need a folder that allows "News" items underneath the Archive folder (create a folder, and create a news item in that folder, and check if it works, if it doesn't, look at the document type of the folder to allows the news doctype), get the id of that archive folder (on the properties tab of the pages in umbraco)
    Change the line int archiveId =1138;  with the ID of the Archive folder on your system.

    Clean up all the stuff you added in your previous post. Restart umbraco and the code should run when you save a new.  

    But I think this is a bit too complicated if you are not used to code to get things running.
    Umbraco is really flexible, but you need to build the stuff you want. 
     

     

Please Sign in or register to post replies

Write your reply to:

Draft