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;
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.
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;
}
}
}
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.
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?
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.
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
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:
which can actually be tidied up to this:
Cheers,
Maff
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
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
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:-
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
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
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
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.
is working on a reply...