Copied to clipboard

Flag this post as spam?

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


  • Craig100 1136 posts 2523 karma points c-trib
    Oct 09, 2014 @ 21:09
    Craig100
    0

    How to access root content from a surfaceController?

    Hi,

    Slightly altering a working email form sending SurfaceController to pick up the "To" email address from a Settings page in the root of the site instead of picking it up from the page that has the form in it.

    This worked when the "emailToAddress" was held in the "Contact" page:

    var currentNode = Umbraco.TypedContent(UmbracoContext.PageId.GetValueOrDefault());
    var toAddress = currentNode.GetProperty("emailAddress").Value.ToString();
    

    However, when trying to access the address in the settings page (a sibling of root) it complains that Model does not exist:-

    var settings = Model.Content.AncestorOrSelf(1).Sibling("Settings");
    var toAddress = settings.GetPropertyValue("emailToAddress").ToString();
    

    I guess it must be possible to access the data I need, I just don't have a clue how to go about it.

    Any advice appreciated,

    Craig

  • Damian Green 452 posts 1433 karma points
    Oct 09, 2014 @ 22:59
    Damian Green
    100

    Hi Craig,

    How about this?

            var settings = this.UmbracoContext.ContentCache.GetAtRoot().Where(x => x.Name == "Settings").FirstOrDefault();
    
            if (settings != null)
            {
                //do your stuff
            }
    

    Also rather than using typedcontent use the cache! :)

    And to get the current page:

    var currentNode = this.UmbracoContext.PublishedContentRequest.PublishedContent;
    

    That any good?

    Damian

  • Damian Green 452 posts 1433 karma points
    Oct 09, 2014 @ 23:05
    Damian Green
    0

    Also the Sibling() extension is looking for contentType alias not name. You didnt say whether setting was your type or name.

    You could do what yu have with a small tweak for type alias:

    Model.Content.AncestorOrSelf(1).Siblings().Where(x => x.ContentType.Alias == "Settings").FirstOrDefault();
    
  • Craig100 1136 posts 2523 karma points c-trib
    Oct 10, 2014 @ 02:30
    Craig100
    0

    Thanks Damian.

    The first one worked thanks. Don't like to use the cache but if there's no other way.

    "Settings" is the alias though would prefer to do it by pageName so can now do either, thanks:)

    As for the second one....... "The name 'Model' does not exist in the current context" still appears on build, (as you would expect?)

    Cheers,

    Craig

  • Damian Green 452 posts 1433 karma points
    Oct 10, 2014 @ 12:00
    Damian Green
    0

    Why wouldn't you want to use the cache?

    Model wont exist in the current context because you are in a surface controller.

    You can use CurerntPage to get the current IPublishedContent - which is the same as the Model.Content in a partial.

    Damian

  • Craig100 1136 posts 2523 karma points c-trib
    Oct 10, 2014 @ 12:02
    Craig100
    0

    Just that cache sounds temporary, might not be there, etc. Not actually sure which cache you mean as you didn't say.

    But thanks anyway. All done and answer marked ;)

    Cheers,

    Craig

  • Damian Green 452 posts 1433 karma points
    Oct 10, 2014 @ 12:21
    Damian Green
    0

    Good stuff...

    Just to clear this up for you:

    The cache is the published content cache of the website - its where umbraco goes to get a webpage when you visit it - so its loading from file rather than db.

    If its published its in the cache (unless something wrong with the cache) :)

    And Umbraco.TypedContent(id) returns IPublishedContent - which again if its not published wont find it.

    If you want to retrieve an item whether its Published or not then use the ContentService Api.

    Hope that makes things clear for you. :)

    Damian

    PS: and the item will remain in the published content cache while ever it is published. Only comes out when you unpublish.

  • Craig100 1136 posts 2523 karma points c-trib
    Oct 10, 2014 @ 13:41
    Craig100
    0

    Ah THAT cache. That's great, thanks for the clarification. Been doing some work on a (non-Umbraco) site using the server cache which was why I was open to "other" caches:)

    Cheers,

    Craig

Please Sign in or register to post replies

Write your reply to:

Draft