Copied to clipboard

Flag this post as spam?

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


  • mark 3 posts 33 karma points
    Jun 16, 2014 @ 17:27
    mark
    0

    How to set default values for properties when creating a new node from document type

    Hello All,

    How can I set a default value for my property so that when I create a new node/document from my document type, it auto populates the value.

    For example:
    Name: Page Title
    Alias: pageTitle
    Type: Textstring

    How do I by default set the page title to untitled every time a new node/document of this document type is created.

    Cheers,

    Mark

  • Andy Butland 410 posts 2298 karma points MVP 4x hq c-trib
    Jun 16, 2014 @ 17:58
    Andy Butland
    103

    You can look at events for this Mark.  I've done something similar using an event handler class something like the following:

    public class InitializeNewMyDocumentTypeContent : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Created += ContentCreated;
            base.ApplicationStarted(umbracoApplication, applicationContext);
        }
    
        private void ContentCreated(IContentService sender, NewEventArgs<IContent> e)
        {
            if (e.Entity.ContentType.Alias == "MyDocumentType")
            {
                e.Entity.SetValue("pageTitle", "Untitled");
                sender.Save(e.Entity);
            }
        }
    }
  • Alastair Todd 44 posts 142 karma points
    May 10, 2016 @ 17:41
    Alastair Todd
    0

    Seems sender.Save(e.Entity); will persist the document

    For those just looking for prepoluation of the form. leave that statement out...

  • Jon R. Humphrey 164 posts 455 karma points c-trib
    Nov 15, 2017 @ 22:42
    Jon R. Humphrey
    0

    Andy,

    3+ yrs later and you still hit the mark!

    Thank you for adding this as it helped me understand quite a few points I had slightly wrong!

    h5yr!

  • Andy Butland 410 posts 2298 karma points MVP 4x hq c-trib
    Nov 16, 2017 @ 07:14
    Andy Butland
    0

    Good to know if it works, but worth flagging that now, I believe these events have been obsoleted - see here.

    So might not be around for the next major version and as those docs indicate may not work in all scenarios.

    You might want to look at content templates now for this type of thing.

    Andy

  • Jon R. Humphrey 164 posts 455 karma points c-trib
    Nov 16, 2017 @ 09:42
    Jon R. Humphrey
    0

    Andy,

    Thanks again for this, I knew about those being obsoleted but at the moment the site is stuck on 7.5.11.

    I'm in talks to update the whole process in the new year so we can use 7.7+ in new projects going forward.

    Content templates are one of the main reasons for this, as well as all the other wonderfulness packed into the system now! :-D

    Cheers for the heads up nonetheless!

    Jon

  • David Armitage 491 posts 2053 karma points
    May 09, 2018 @ 01:36
    David Armitage
    0

    Great. Thanks.

    The only thing I would mention which I think someone else touched on is that the sender.Save(e.Entity); will actually fail when creating new nodes. The reason for this is the blank node does not yet have a name.

    1. You can either set the name and the same will be ok.

    2. Or like someone previously said just delete that line of code and don't run the save.

  • Dennis Aaen 4495 posts 18202 karma points admin hq c-trib
    Jun 16, 2014 @ 18:39
    Dennis Aaen
    0

    Hi Mark,

    Dependents on what version of Umbraco you are using there are some packages for handling of standard values on document types, I donĀ“t know if there are any package for this to version 7.

    By here are some of the package that are available for previous versions of Umbraco

    Standard Values in Umbraco: http://our.umbraco.org/projects/developer-tools/standard-values-in-umbraco

    Default Values for Umbraco: http://our.umbraco.org/projects/developer-tools/default-values-for-umbraco

    Maybe this can be useful to you.

    /Dennis

     

  • mark 3 posts 33 karma points
    Jun 16, 2014 @ 20:52
    mark
    0

    Hey Dennis, many thanks for your answer but Andy has hit the nail on the head as thats exactly what i was looking for. Works great!

  • Nicholas Westby 2045 posts 7034 karma points c-trib
    May 10, 2016 @ 19:12
    Nicholas Westby
    0

    In case anybody else is curious, a coworker of mine created this plugin for Umbraco 7 that does pretty much what Andy said (side note: humorously, my coworker is also named Andy), but it bases the new content nodes on existing content nodes (i.e., you can edit the default content in Umbraco):

    https://our.umbraco.org/projects/backoffice-extensions/contenttemplates/

  • John M. 18 posts 112 karma points
    Jul 27, 2018 @ 15:41
    John M.
    0

    Hello everybody.I had a similar issue.I have tried this solution and it works for me for textstrings. BUT, what if you want to do this in a dropdown list???How to set default value in this control?

Please Sign in or register to post replies

Write your reply to:

Draft