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]++;
}
}
}
}
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
Anybody know how to do this?
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
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..
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 calledsections
. 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.
Hope that helps ;)
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
is working on a reply...