Copied to clipboard

Flag this post as spam?

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


  • mukund rayate 22 posts 172 karma points
    Jun 08, 2016 @ 09:25
    mukund rayate
    0

    What is best way to access global site settings value ?

    I Have site setting Document type with Add at root node is set , I am creating one page with that which is added at root level , now i want to access that page data throughout my all views and macro . Please suggest me which is best way to do it ?

  • David Armitage 510 posts 2081 karma points
    Jun 09, 2016 @ 05:33
    David Armitage
    1

    Hi,

    For site settings I follow the same path as you. Add a node at the root of the site.

    The second half of the puzzle I create a class to store all my settings values.

    E.g

    public class SiteSettings
    {
        //general settings
        public string SiteName { get; set; }  
        public string SiteUrl { get; set; }
        public string SiteDisplayUrl { get; set; }
        public string SocialMediaShareImage { get; set; }
        public int BlogArticlesPerPage { get; set; }
        public string BlogListPage { get; set; }
    
        public SiteSettings()
        {
            var node = ContentManager.GetNodeByNodeTypeAlias("siteSettings");
            Parse(node);
        }
    
        public void Parse(Node node)
        {
            if (node != null)
            {
                //site settings
                SiteName = node.GetProperty("siteName") != null ? node.GetProperty("siteName").Value : string.Empty;
                SiteUrl = node.GetProperty("siteUrl") != null ? node.GetProperty("siteUrl").Value : string.Empty;
                SiteDisplayUrl = node.GetProperty("siteDisplayUrl") != null ? node.GetProperty("siteDisplayUrl").Value : string.Empty;
                SocialMediaShareImage = node.GetProperty("socialMediaShareImage") != null ? MediaManager.GetMediaUrlById(int.Parse(node.GetProperty("socialMediaShareImage").Value), SiteUrl, true) : string.Empty;
                BlogArticlesPerPage = node.GetProperty("blogArticlesPerPage") != null ? int.Parse(node.GetProperty("blogArticlesPerPage").Value) : 5;
                BlogListPage = node.GetProperty("blogListPage") != null ? node.GetProperty("blogListPage").Value : string.Empty;
            }
        }
    }
    

    Then whenever in need the site settings either in my backend or my razor script I just call.

    SiteSettings siteSettings = new SiteSettings();

    Quick and easy. You can probably improve it by adding your own caching but I think Umbraco caches this stuff pretty well already.

    Oh and here is the helper method I created to get the siteSettings node

    public static Node GetNodeByNodeTypeAlias(string nodeTypeAlias)
    {
        return uQuery.GetNode(-1).GetDescendantNodes().Where(x => x.NodeTypeAlias == nodeTypeAlias).FirstOrDefault();
    }
    

    Hope this helps

    Kind Regards

    David

  • mukund rayate 22 posts 172 karma points
    Jun 09, 2016 @ 05:55
    mukund rayate
    0

    Thanks for your help.
    initially i was trying to create model but i cant inherit it to view.

    but now

    i am using your approach , is there any extra assembly need for same for "ContentManager"

  • David Armitage 510 posts 2081 karma points
    Jun 09, 2016 @ 08:54
    David Armitage
    100

    Hi,

    No there is no extra assembly for ContentManager. This is one of my custom classes which contains a few helper methods I created a speed things up for me. Same for MediaManager. Just ignore this. I copied and pasted straight out of one of my projects.

    In my example that line of code is just calling this method.

    public static Node GetNodeByNodeTypeAlias(string nodeTypeAlias)
    {
        return uQuery.GetNode(-1).GetDescendantNodes().Where(x => x.NodeTypeAlias == nodeTypeAlias).FirstOrDefault();
    }
    
  • mukund rayate 22 posts 172 karma points
    Jun 09, 2016 @ 09:03
    mukund rayate
    0

    Thanks David it work for me . :)

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Jun 09, 2016 @ 10:29
    David Brendel
    0

    Hi,

    a little late but I mostly go with having a content picker on my root page with which I select the settings page and then get it by the selected id. This can then also be done recursivly.

    Regards David

Please Sign in or register to post replies

Write your reply to:

Draft