Copied to clipboard

Flag this post as spam?

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


  • Siw Ørnhaug 130 posts 109 karma points
    Jan 21, 2014 @ 17:43
    Siw Ørnhaug
    0

    Archive all news items except 10 newest in news folder

    Hi guys,

    this should be simple, but I very rarely code things in Umbraco, so I'd like to know what's the best solution.

    Customer has Umbraco 4.7.2 - not going to upgrade for a while yet. 

    I'd like to create an event handler on the beforePublish event and I've found code that works:
    http://www.richardsoeteman.net/PermaLink,guid,f470b6cf-40da-4aa9-a0d9-7b984fe9bf59.aspx

    My problem is I need to sort all documents in folder by a custom property "dato" and keep the 10 newest in the folder, the rest go to archive folder. Unpublished documents I think will not be touched. 

    A code sample to do this would be much appreciated :)

     

  • Siw Ørnhaug 130 posts 109 karma points
    Jan 22, 2014 @ 14:04
    Siw Ørnhaug
    0

    I'm testing. Does anyone know why this moves only 1 item?

                int nyhetsid = 3156;  //1070;
                int arkivid = 3281;  //2058;
    
                // Logg alltid eventer!
                Log.Add(LogTypes.Custom, sender.Id, "Document Before publish Raised");
    
                // Sjekk om har dokumenttype nyhet og at den skal arkiveres
    
                if (sender.ContentType.Alias == "Sak")
                {
                    // Sjekk at er i nyhetsmappen
    
                    if (sender.ParentId == nyhetsid)
                    {
                        Document nyhetsmappe = new Document(nyhetsid);
    
                        // Lag en liste med alle publiserte elementer i mappen
                        if (nyhetsmappe.HasChildren)
                        {
                            int i = 0;
                            Node mappe = new Node(nyhetsmappe.Id);
                            DataView dataView = new DataView(mappe.ChildrenAsTable());
                            dataView.Sort = " dato DESC";
    
                            foreach (DataRowView view in dataView)
                            {
                                int id = Convert.ToInt32(view.Row["Id"].ToString());
                                Document d = new Document(id);
                                if (d.Published)
                                {
                                    i++;
                                    if (i == 1)
                                    {
                                        d.Move(arkivid);
    
                                        // Avpubliser fra gjeldende node
    
                                        umbraco.library.UnPublishSingleNode(d.Id);
                                        d.UnPublish();
    
                                        // Publish blir kalt hvis brukeren velger Save and publish
                                    }
                                }
                            }
                        }
                    }
                }
Please Sign in or register to post replies

Write your reply to:

Draft