Copied to clipboard

Flag this post as spam?

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


  • Paul de Quant 403 posts 1520 karma points
    Aug 10, 2018 @ 16:06
    Paul de Quant
    0

    Is it possible to see how many times a grid control has been used in the site

    Hello,

    As the title suggests, is it possible to find out how many times a specific grid control has been used. Not just in a page but in the site?

    Thanks

    Paul

  • Paul de Quant 403 posts 1520 karma points
    Aug 13, 2018 @ 14:26
    Paul de Quant
    0

    Anybody know how to do this?

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Aug 13, 2018 @ 14:39
    Nik
    0

    Hi Paul,

    I don't believe there is an easy way to do this I'm afraid. I think you'd have quite a challenge as the grid data is stored in the database in Json.

    Nik

  • Ravi Motha 290 posts 500 karma points MVP 7x c-trib
    Aug 13, 2018 @ 15:29
    Ravi Motha
    0

    I'm not sure any of the cool tools out there will do this: but I think: this may be a good starting point..

    https://offroadcode.com/packages/back-office-visualization/

    I'm sure the giys and gals at @Offroadcode would be interested in extending this..

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Aug 13, 2018 @ 15:39
    Anders Bjerner
    0

    Hi Paul,

    If this is a task you just run once in a while, I would simply loop through all content and and their properties. Even for sites with thousands of content items, this not take more than a handful of seconds.

    With native Umbraco, a grid property should have a value of the type JObject, which again should have a property called sections. This should let you distinguish grid properties from other properties with JSON data.

    When you've found the grid property, you'll then have to parse the JSON a bit further to get the alias of each grid editor. This may be a bit comples, so I've gone ahead and create an example that does this for you.

    It will then list each editor alias along with the amount of times each of them have been used.

    @using Newtonsoft.Json.Linq
    @inherits UmbracoViewPage
    
    @{
    
    
        Dictionary<string, int> editors = new Dictionary<string, int>();
    
    
        foreach (IPublishedContent content in UmbracoContext.ContentCache.GetAtRoot()) {
    
            Magic(editors, content);
    
            foreach (IPublishedContent descendant in content.Descendants()) {
    
                Magic(editors, descendant);
    
            }
    
        }
    
        foreach (var pair in editors.OrderByDescending(x => x.Value)) {
            <pre>@pair.Key => @pair.Value</pre>
        }
    
    
    
    }
    
    @functions {
    
        public void Magic(Dictionary<string, int> editors, IPublishedContent content) {
    
            foreach (var property in content.Properties) {
    
                JObject json = property.Value as JObject;
                if (json == null) continue;
    
                // Skip the property if it doesn't look like a grid property
                if (!(json.GetValue("sections") is JArray)) continue;
    
                // Find each editor alias using JPath
                foreach (string editorAlias in json.SelectTokens("sections[*].rows[*].areas[*].controls[*].editor.alias")) {
    
                    if (!editors.ContainsKey(editorAlias)) editors[editorAlias] = 0;
    
                    editors[editorAlias]++;
    
                }
    
            }
    
        }
    
    }
    

    Hope that helps ;)

  • Paul de Quant 403 posts 1520 karma points
    Aug 13, 2018 @ 15:47
    Paul de Quant
    0

    Hi Everyone,

    Thanks for all your suggestions. Anders, I'll have to try your code to see if it does the job.

    I'll let you know if it works.

    Thanks

    Paul

Please Sign in or register to post replies

Write your reply to:

Draft