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
    Feb 03, 2015 @ 01:29
    Craig100
    0

    Access root settings from own class

    V7.2.2

    Content
    ----en-GB (site)
    ----de-de (site)
    ----Global Settings (settings)

    From a View or Partial I can use: var settings = this.UmbracoContext.ContentCache.GetAtRoot().Where(x => x.Name == "Global Settings").FirstOrDefault(); to reach settings in the Global Settings page.

    In my source I have a /classLib directory with myClass.cs in it.  The class methods are called from a custom default controller. How do you access the Global Settings from the myClass file? Intellisense isn't helping :(

    I have these usings available:-

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web;
    using Newtonsoft.Json;
    using Archetype.Models;
    using Archetype.Extensions;
    using Archetype.PropertyConverters;

    Thnx,

    Craig

  • Maff 141 posts 466 karma points
    Feb 03, 2015 @ 07:41
    Maff
    0

    Hi Craig,

    You can use the ContentService API to do this - you will need to reference Umbraco.Core in your class and then you can use:

    var settings = ApplicationContext.Current.Services.ContentService.GetRootContent().Where(x => x.Name == "Global Settings").FirstOrDefault();

    which can actually be tidied up to this:

    var settings = ApplicationContext.Current.Services.ContentService.GetRootContent().FirstOrDefault(x => x.Name == "Global Settings");

    Cheers,

    Maff 

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 03, 2015 @ 09:26
    Jeroen Breuer
    1

    I wouldn't use the content service for this because it doesn't use a cached version which means it's slower. Try doing the same with the Umbraco helper. If your controller inherits from the surface or render mvc controller you should already have the helper. 

    I wrote some extension methods for the helper: https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Extensions/Utilities/ExtensionMethods.cs#L217

    Jeroen

  • Craig100 1136 posts 2523 karma points c-trib
    Feb 03, 2015 @ 12:29
    Craig100
    0

    Thanks both.

    In trying to set up Jeroen's Umbraco.Extensions.Utilities ExtensionMethods class "Website" (on it's own), I've got it down to build with just 2 errors which I can't get rid of.:-

    Error1Extension method must be defined in a non-generic static classC:\VS2012\test\test\classLib\ExtensionMethods.cs28

    Error2'Umbraco.Extensions.Utilities.ExtensionMethods.Website(Umbraco.Core.Models.IPublishedContent, bool)' is a 'method' but is used like a 'type'C:\VS2012\test\test\classLib\ExtensionMethods.cs38

    I'm guessing I'm missing a "using" statement. I got it down from 12 errors by adding all your usings and then commenting them out till I got down to a minimum number. As "Global Settings" is a root node, I'm guessing it's name can be substituted for the generic "Website" which you had originally. 

    Code is:-

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    using Umbraco.Core.Models;
    using Umbraco.Web;
    
    namespace Umbraco.Extensions.Utilities {
        public class ExtensionMethods {
    
            private static UmbracoHelper Umbraco
            {
                get { return new UmbracoHelper(UmbracoContext.Current); }
            }
    
            /// <summary>
            /// Return the Website generated model (highest node) where global settings are stored.
            /// </summary>
            public static Website Website(this IPublishedContent content, bool noCache = false)
            {
                var website = (Website)System.Web.HttpContext.Current.Items["Global Settings"];
    
                if (website == null || noCache)
                {
                    website = content.AncestorOrSelf(1) as Website;
    
                    if (!noCache)
                    {
                        System.Web.HttpContext.Current.Items["Global Settings"] = website;
                    }
                }
    
                return website;
            }
    
        }
    }
  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 03, 2015 @ 12:34
    Jeroen Breuer
    0

    For error 1: Your ExtensionMethods needs to be static. So public static class ExtensionMethods.

    For error 2: I'm not really sure what goes wrong. Also website = content.AncestorOrSelf(1) as Website; only works if you are using the ModelsBuilder. It's just an example. It won't work if you try to copy it.

    Jeroen

  • Craig100 1136 posts 2523 karma points c-trib
    Feb 03, 2015 @ 12:48
    Craig100
    0

    Thanks Jeroen. From your experience, if I install the ModelsBuilder to get this working, will it interfere with anything else and cause massive pain elsewhere to existing code?

    Thnx

    Craig

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 03, 2015 @ 12:49
    Jeroen Breuer
    0

    Hi,

    It might be better to have a look at the Hybrid Framework. It comes with the ModelsBuilder and also has the Website extension method.

    Jeroen

  • Craig100 1136 posts 2523 karma points c-trib
    Feb 03, 2015 @ 13:07
    Craig100
    0

    Sorry but as usual I don't have that much time to complete the site. So would be very nervous to get into the Hybrid Framework at this stage with all the re-learning I'm guessing I'd have to do. Not so bad if not in such a rush, but then I always am :( Will have to have a play with it in slow time at a later date.

    Thanks anway.

Please Sign in or register to post replies

Write your reply to:

Draft