Copied to clipboard

Flag this post as spam?

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


  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 05, 2015 @ 17:59
    Jeroen Breuer
    0

    Auto create media folders in Umbraco 7

    Hello,

    When a content node is created I also want to create a folder with the same name in the media section. This can be done with events, but in Umbraco 7 there is a problem. The node name won't be available until after the content is created. This is a problem because while creating the content the user should already be able to use the media folder with the media pickers. 

    In Umbraco 6 you first choose a name before creating content. That way you could already create the media folder with the name in the created event, but in Umbraco 7 the name isn't available in that event.

    Does anybody have a suggestion for this problem? Could I inject some custom javascript in the backoffice that after the name has been entered at the top an ajax call creates the media folder? 

    Jeroen

  • Marcel van Helmont 68 posts 259 karma points c-trib
    Jan 05, 2015 @ 20:37
    Marcel van Helmont
    0

    Jeroen,

    If you extend the media picker to create a folder in de media section if not exist. May be thats a solution!

  • Søren Kottal 702 posts 4497 karma points MVP 5x c-trib
    Jan 05, 2015 @ 20:46
    Søren Kottal
    0

    Not any help to you, but I really think Umbraco should go back to the old "name-your-node-before-creating" concept.

    Every time I have had new editors in for training, they forget to name their nodes before saving.

  • Marcel van Helmont 68 posts 259 karma points c-trib
    Jan 05, 2015 @ 20:53
    Marcel van Helmont
    0

    Jeroen,

    You can also hookup to the ContentService_Saving event and check the HasIdentity property on the entity.

    Something like this:

    void ContentService_Saving(IContentService sender, global::Umbraco.Core.Events.SaveEventArgs<IContent> e)
        {
            foreach (var item in e.SavedEntities)
            {
                if (!item.HasIdentity)
                {
                    var mediaService = ApplicationContext.Current.Services.MediaService;
                    var f = mediaService.CreateMedia(item.Name, Constants.System.Root, "Folder");
    
                    mediaService.Save(f);
    
                }
            }
        }
    
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jan 05, 2015 @ 21:01
    Dennis Aaen
    0

    @Søren If you try to save nodes without a name in Umbraco 7 you will get a required message in the field where you typed the name of the page. So to me you can´t save a page without giving it a name right if you are using the Umbraco backoffice?

    /Dennis

  • Søren Kottal 702 posts 4497 karma points MVP 5x c-trib
    Jan 06, 2015 @ 08:04
    Søren Kottal
    0

    You are right Dennis. Its just an annoyance for the editors I have trained.

  • Søren Kottal 702 posts 4497 karma points MVP 5x c-trib
    Jan 06, 2015 @ 08:05
    Søren Kottal
    0

    @Jeroen, can you create the folder with the new content id as a name, and then rename when you have the name?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 06, 2015 @ 09:30
    Jeroen Breuer
    0

    Thanks for all the suggestions, but I'm afraid none of them will work.

    The ContentService_Saving is called after all mandatory fields have been filled which includes the media picker that needs the folder. 

    Naming the media folder the id also won't work because that also isn't available in the created event.

    I could create a folder with just a temp name (not based on the id or name) and rename that later. The problem is that I would like to rename it as soon as possible. I'll do some experimenting to see if I can change the folder name after the node name has been changed. So that will probably need a custom ajax call in the backoffice.

    Jeroen

  • Marcel van Helmont 68 posts 259 karma points c-trib
    Jan 06, 2015 @ 10:02
    Marcel van Helmont
    1

    He Jeroen,

    A you need to create the folder on the same moment you create a new page.

    May be you can move/kopie the selected media items to the correct folder after saving te new page.

    Marcel

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 06, 2015 @ 10:12
    Jeroen Breuer
    0

    Creating the folder at the same moment as creating a new page is the whole problem. When the page is create the name and id aren't available yet in Umbraco 7 (it was in Umbraco 6) so I don't know what to name the folder. 

    The move/copy option of the selected media items isn't a bad idea. Thanks for the suggestion :-).

    Jeroen

  • Søren Kottal 702 posts 4497 karma points MVP 5x c-trib
    Jan 06, 2015 @ 10:16
    Søren Kottal
    0

    Maybe generate a Guid as a temporary folder name?

  • Marcel van Helmont 68 posts 259 karma points c-trib
    Jan 06, 2015 @ 10:23
    Marcel van Helmont
    0

    The problem is when you create a folder on the create event than what to do when you leave the page and not hitting save/publish.. At the create event there is nothing saved to the database so i think it's not the right please to create the folder..

    Good luck

    Marcel

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 06, 2015 @ 10:24
    Jeroen Breuer
    0

    That's what I'm currently doing, but in that situation the user doens't know what that folder is. For example:

    Content:

    - Home
    -- new content page that doesn't have a name yet

    Media:

    - Home
    -- Guid folder which the user doesn't know is the folder of the content page he's currently creating

    So I somehow need to rename the folder at the moment that the content name is available. 

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 07, 2015 @ 11:25
    Jeroen Breuer
    0

    I'm now trying to insert some javascript which should run each time a content page is loaded. You can insert javascript into the backoffice like this: https://github.com/leekelleher/umbraco-environment-indicator/blob/master/src/App_Plugins/CmsEnvironmentIndicator/package.manifest

    Does anyone know what I need to do for my code to run each time a content page is loaded? 

    Jeroen

  • Jon R. Humphrey 164 posts 455 karma points c-trib
    Jan 07, 2015 @ 11:57
    Jon R. Humphrey
    0

    Jeroen,
    If you're using jquery then set the methods inside the js file with a domready function call:

    $(function(){
    var myFunction = function(){
    //you're code
    });
    });

    However if you're using vanilla javascript you can set the methods with a window.onload call like this:

    window.onload="myFunction()";


    OR

    myFunction()
    

    Please make sure Lee's manifest helper loads the scripts at the bottom of the html no matter what:

    <script type="text/javascript" src="/forum/your/path/FileName.js"></script>
    </body>
    </html>
  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 07, 2015 @ 12:05
    Jeroen Breuer
    0

    I'm already using Warren his example: https://gist.github.com/warrenbuckley/07d21b682c8e076cb5e2

    As he suggested on Twitter I need to find an event which I can use so my code runs each time a content page is loaded.

    Jeroen

  • Jon R. Humphrey 164 posts 455 karma points c-trib
    Jan 07, 2015 @ 12:11
    Jon R. Humphrey
    0

    Jeroen,
    I see what Warren has done, however as Shannon says you shouldn't mix serverside with clientside? If you're trying to load a javascript function into any page as soon as the html parser reaches the script call it's fired? This is why it's advised to move scripts to the bottom of the html or to use the domready functionality of jquery - to stop the parser from pausing while the script its run and therefore pausing the pageload; or am I missing the whole point?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 07, 2015 @ 12:14
    Jeroen Breuer
    0

    With the package.manifest solution Umbraco handles adding the javascript file. So I've already got a javascript file that runs, but the problem is that I need to run some custom javascript when a content page is loaded.

    Jeroen

  • Piotr Bach 15 posts 129 karma points c-trib
    Oct 13, 2021 @ 09:14
    Piotr Bach
    1

    Hi, the thread is quite old, but the problem is still current. Content editors still have to create folders themselves to keep the media section tidy. However, Dedicated Media Folder package solves the problem.

Please Sign in or register to post replies

Write your reply to:

Draft