Copied to clipboard

Flag this post as spam?

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


  • Jim 26 posts 86 karma points
    Jun 29, 2015 @ 07:12
    Jim
    0

    write once, use many

    Hi

    This is a very simple requirement but I can't seem to find a simple solution anywhere.

    I want the user to be able to enter text that will appear on all pages. If I add properties to my master page, the user must enter text for each child page that is created. I want them to be able to enter the text once only, as it is the same text that appears on each page.

    What is the simplest structure I can use to achieve this?

    Thanks

    Jim

  • Stefano Beretta 102 posts 247 karma points
    Jun 29, 2015 @ 07:28
    Stefano Beretta
    0

    Hi Jim, you can put a property on the root node of your application and then use

    Model.Content.GetPropertyValue("propertyAlias", true) 
    

    where the second parameter is "isRecursive?"

    Hope this help

    Stefano

  • Jim 26 posts 86 karma points
    Jun 29, 2015 @ 07:35
    Jim
    0

    Hi Stefano

    Thanks very much for your reply.

    I'm not sure I understand what you mean by 'put a property on the root node of your application'. Could you please explain a little more?

    Thanks

    Jim

  • René Pjengaard 118 posts 702 karma points c-trib
    Jun 29, 2015 @ 07:36
    René Pjengaard
    0

    Hi Jim,

    you will have to create a property on the root-level documentType (or where you want it to inherit from).

    In your view, you then use the recursive option in the GetProperty method:

    @(Model.GetProperty("propertyAlias", true))
    

    The propertyAlias string will be your propertyalias on the root documenttype and the boolean true, makes the call recursive.

    /René

  • Stefano Beretta 102 posts 247 karma points
    Jun 29, 2015 @ 07:39
    Stefano Beretta
    0

    Hi Jim,

    I think René explained better what I was suggesting you.

    Is that the solution?

    BR

    Stefano

  • Jim 26 posts 86 karma points
    Jun 29, 2015 @ 07:45
    Jim
    0

    Thanks both.

    If I put the property on my master page (which is inherited by all other pages), the property will appear on the content nodes of all pages. The user will then have to enter the same text multiple times, which is not what I want.

    I think I'm still missing something.....

    Jim

  • Stefano Beretta 102 posts 247 karma points
    Jun 29, 2015 @ 08:03
    Stefano Beretta
    0

    Hi Jim, you are correct.

    If the doctype strucuture of your website is:

    • Master doctype (here the property)

      • Doctype 1
      • Doctype 2
      • etc

    so yes, the property will appear on every node.

    I usually create a doctype ad hoc for my root node, so I can add the properties reserved for it:

    • Master doctype

      • Home doctype (here the property)
      • Doctype 2
      • etc

    If you don't want to change doctype to your root node you can add the property to your master doctype and then use the package "backoffice tweaking" http://our.umbraco.org/projects/collaboration/backoffice-tweaking to hide the property from all other doctypes but the root node doctype.

    BR Stefano

  • Jim 26 posts 86 karma points
    Jun 29, 2015 @ 08:17
    Jim
    0

    Hi Stefano

    My structure is not hierarchical in this way. Rather than create doctype1, 2 etc as children of my master doctype I am using document type compositions to specify the relationship. One thing I have tried is to create a common data doctype at the same level as everything else, like so:-

    • Master doctype
    • Doctype 1
    • Doctype 2
    • CommonData doctype

    Doctypes 1 and 2 have master doctype checked in the document type compositions section. I'm not sure how to bring the common data document type into the relationship....

    Jim

  • Anthony Chudley 50 posts 197 karma points
    Jun 29, 2015 @ 08:57
    Anthony Chudley
    0

    As such Jim it doesn't matter if it happens to be on every item of content.

    The recursive lookup will look up the content tree till it finds an item with a value.

    So if it is filled in on your home node and blank on any created under it, it will look up the tree till it happens to find it.

    -Home - Has property value

    --Child 1 - Blank property value

    --Child 2 - Blank property value

    In this case, Child 1 and Child 2 would display the value from the home node.

    However if it was given a value on Child 2 it will then display what has been entered on Child 2, and the children under this will have that value (provided they too are left blank).

    I tend to have a Home document type, so it makes it easier to set this at this level as it only appears on the one node, so the user can't accidentally overwrite it a a lower level.

    Perhaps another option might be to use a dictionary item instead, depending on what sort of text you are envisaging.

  • René Pjengaard 118 posts 702 karma points c-trib
    Jun 29, 2015 @ 08:59
    René Pjengaard
    1

    Hi Jim,

    in that case you can always get data from a node by loading this node in your razorview like:

    IPublishedContent settings = UmbracoContext.ContentCache.GetById(1234);
    

    Where 1234 is the id of the CommonData doctype.

    /René

  • Jim 26 posts 86 karma points
    Jun 29, 2015 @ 09:20
    Jim
    0

    Thanks for your replies, Rene and Anthony.

    Anthony - I may be able to restructure my site to use your solution but as things stand all pages are at the same level (root). Although they all inherit from the same master page doctype (via document type compositions), the structure is currently flat.

    This means that each page is a sibling rather than a child and I'm guessing that if the user enters the common data on one sibling this data would not be available to the other siblings via the recursive lookup mechanism (would it?)

    Jim

  • Anthony Chudley 50 posts 197 karma points
    Jun 29, 2015 @ 09:26
    Anthony Chudley
    0

    That's correct Jim, the recursive property only works in an up direction and not sideways.

    In that case René's last suggestion is probably going to be the best answer for you in this situation. Having a separate item and getting the information directly from that.

  • Mike Chambers 636 posts 1253 karma points c-trib
    Jun 29, 2015 @ 09:48
    Mike Chambers
    0

    If its just a simple text string why not use a dictionary item?

  • Jim 26 posts 86 karma points
    Jun 29, 2015 @ 09:53
    Jim
    0

    Hi Anthony

    I decided to try your solution first.

    Unfortunately, when I place the following code snippet in my master page template:-

    @(Model.GetProperty("propertyAlias", true))

    I get the following error message:-

    Compiler Error Message: CS1501: No overload for method 'GetProperty' takes 2 arguments
    

    I have also tried putting the code snippet in a partial view but the result is the same.

    Up until this point I was using the field inclusion snippet

    @Umbraco.Field("propertyAlias")

    Is there a recursive variant of this I should be using?

    Jim

  • Anthony Chudley 50 posts 197 karma points
    Jun 29, 2015 @ 10:01
    Anthony Chudley
    101

    I'm using this:

    @Umbraco.Field("propertyAlias", recursive: true)
    

    Seems to work for me.

    Although as Mike said, if it's a simple text string a Dictionary Item might be easier.

  • Jim 26 posts 86 karma points
    Jun 29, 2015 @ 10:49
    Jim
    0

    Excellent - that works for me too!

    I've made a note of all the other suggestions for further experimentation.

    Thanks to all those who responded.

    Jim

Please Sign in or register to post replies

Write your reply to:

Draft