Copied to clipboard

Flag this post as spam?

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


  • Matthew Jackson 17 posts 36 karma points
    Sep 26, 2012 @ 07:38
    Matthew Jackson
    0

    Programatically create mirror of content tree within media.

    Hi all,

    I am a umbraco newbie been playing with it for 2 weeks now. What an amazing templating engine.  Since integrating DAMP I have a requirement where I want to store pageicons for each page, also I would like to store my media under a heirachy mirroring my content tree.  I am familiar with event handlers and have found the following code which has got me started however I am stumped where to go from here.

     

    using System;
    using System.Collections.Generic;

    using System.Web;

    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic;
    using umbraco.cms.businesslogic.media;
    using umbraco.cms.businesslogic.web;
    using umbraco.cms.businesslogic.property;


    using System.Web.Security;
    using System.Linq;


        public class CreateClientMediaFolder : umbraco.BusinessLogic.ApplicationBase
    {
        
            public CreateClientMediaFolder()
            {
                Document.AfterPublish += new Document.PublishEventHandler(Document_AfterPublish);
               
        }
        
            void Document_AfterPublish(Document sender, PublishEventArgs e)
            {
        
                string NewContentName = sender.Text;

                    MediaType folderType = MediaType.GetByAlias("folder");
                    //initialise collection of type media where the first or default item is equal to 'ContentImages'.
                    Media Media1 = Media.GetMediaOfMediaType(folderType.Id).FirstOrDefault(m => m.Text == "ContentImages" && m.ChildCount >= 0);
                    
                    //if I have the parent folder details
                    if (Media1 != null)
                    {
                        //initialise new media item
                        Media newFolder;
                        //if parent folder doesn't contain NewContentName
                        if(Media1.Children.FirstOrDefault(c => c.Text == NewContentName) == null)
                            
                            //create new folder mirroring the newly saved page.
                            newFolder = Media.MakeNew(NewContentName, folderType, User.GetUser(0), Media1.Id);
                        }
        
            }
        
        }

     

    Currently I have a tested solution where on newDocument or Document_Afterpublish event a new folder is created under contentImages parent node if not already existing.  I want to extend this to allow a complete mirror of the content tree gracefully.

    Have anyone ever tried this before.  Some direction would be appreciated.

    Thanks

     

     

  • Matthew Jackson 17 posts 36 karma points
    Sep 26, 2012 @ 08:08
    Matthew Jackson
    0

    update: I need to find out nodename of the parent document.  Then I can passthis to, where ParentName is same as parent node. This should then create the folder in the corresponding media node.

    Media Media1 = Media.GetMediaOfMediaType(folderType.Id).FirstOrDefault(m => m.Text == "ParentName" && m.ChildCount >= 0);

     

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies