Copied to clipboard

Flag this post as spam?

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


  • ben_a 65 posts 123 karma points
    Jul 23, 2009 @ 21:55
    ben_a
    0

    Auto-delete content

    Has anyone used the API to "clean-up" content? I have a couple of folders that I want content to disappear from once it has passed its unpublish date.

    I know there's a delete method that will move a node to the trash, and that's good enough for me.

    Before I develop something from scratch, I wanted to see if anyone had already developed something like this that they would share. This is a small part of a bigger project and I don't want to spend too much time on it.

    Thanks,

    Ben

  • Petr Snobelt 923 posts 1535 karma points
    Jul 23, 2009 @ 22:48
    Petr Snobelt
    100

    Maybe you can try use http://our.umbraco.org/projects/content-maintenance-dashboard-package and delete unpublished nodes.

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Jul 24, 2009 @ 10:55
    Douglas Robar
    4

    An option that would automate the process entirely would be  to create a small .net control that uses the umbraco api to find and delete (put in the trash) pages that meet your specific requirements. This will be a pretty small bit of code.

    Then, put the dll in the /bin folder of your umbraco site and the .ascx in the /usercontrols folder; create a new macro in umbraco and select your usercontrol.

    Now that you've got a macro, add it to a page on your site. Be sure to hide the page from navigation (with the umbracoNaviHide property) or place it outside your site's content hierarchy. Just be sure website visitors won't find the page as if it were a regular content page (though even if they did it wouldn't hurt anything, it just doesn't display anything useful for website visitors).

    Then, set a scheduled task in the /config/umbracoSettings.config file to call your page periodically. When the page is run the macro fires and any expired content will be deleted. Run it once a week, once a day, or every hour depending on how precisely you want the content to be moved to trash.

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

     

    cheers,
    doug.

  • ben_a 65 posts 123 karma points
    Jul 24, 2009 @ 14:37
    ben_a
    0

    Thanks Doug!

    I was looking for a way to keep everything "inside" Umbraco without relying on windows task scheduler or creating a service. Your solution looks to be self contained and portable.

    Regards,

    Ben

  • ben_a 65 posts 123 karma points
    Jul 24, 2009 @ 14:42
    ben_a
    0

    Oh and thank Petr for your suggestion. I may not be able to use the dashboard control for my project, but the code-behind will give me some insight on how to mass-manipulate content using the API.

  • ben_a 65 posts 123 karma points
    Jul 24, 2009 @ 21:42
    ben_a
    0

    Here's what I came up with for my user control. I think it's rather database intensive, but shouldn't be bad if I run it once a day. :

    I'm passing in the nodeID value from my macro as a parameter. I want all children underneath the specified node to be deleted once they've expired.

    public partial class COTContentCleaner : System.Web.UI.UserControl
    {

    private int nodeID = 3069;
    public int NodeID
    {
    get { return nodeID; }
    set { nodeID = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    Log.Add(LogTypes.System, -1, "COT Umbraco Content Cleaner Activated");

    if (!umbraco.content.Instance.isInitializing)
    {
    int parentID=0;
    String output = "";
    foreach (Document d in Document.GetDocumentsForExpiration())
    {
    if (d.Level >1 ) parentID =(int)d.Parent.Id;
    if (parentID == nodeID)
    {
    d.UnPublish();
    d.delete();
    output += d.Id.ToString() + " " + d.Text.ToString() + " -deleted" + "<br />";
    }

    }
    outputText.Text = output;
    Log.Add(LogTypes.System, -1, "COT Umbraco Content Cleaner Finished");
    }
    else
    {
    outputText.Text = "Operation Failed. Umbraco is Initializing";
    }
    }
    }
  • Connie DeCinko 931 posts 1160 karma points
    Oct 05, 2016 @ 23:18
    Connie DeCinko
    0

    This looks great. Any idea if this code will still work on Umbraco 6.x?

Please Sign in or register to post replies

Write your reply to:

Draft