1

I am unable to edit cacheable fields via API and if I edit once from the content editor then I can edit via API. Trying to enable cacheable for all renderings via API at the rendering level. These fields are not in the FIELDS property unless the cacheable section is edited ONCE via the content editor.

The item is of data template /sitecore/templates/System/Layout/Renderings/Controller rendering

I am able to enable or disable cacheable and vary by data AFTER the cacheable section fields are edited once via the content editor however the below code fails for any renderings whose cacheable properties aren't edited after it's creation.

    var rootRendering = Database.GetDatabase("master").GetItem("/sitecore/layout/Renderings");
    if (rootRendering != null)
    {
        var renderings = rootRendering.Axes.GetDescendants();           
        foreach (var item in renderings)
        {               
                item.Editing.BeginEdit();
                item.Fields.ReadAll();
                item.Fields["Cacheable"].Value = "1";
                item.Fields["VaryByData"].Value = "1";                    
                item.Editing.EndEdit();
        }
    }

enter image description here

enter image description here

6
  • Are you trying to edit the rendering item?\
    – Richard Seal
    Commented Mar 6, 2021 at 0:03
  • 1
    Rendering Parameters are not items and don't have fields
    – Mark Cassidy
    Commented Mar 6, 2021 at 1:21
  • @RichardSeal Yes, trying to enable cacheable properties at a rendering level.
    – SNew
    Commented Mar 6, 2021 at 15:24
  • @MarkCassidy I am referring to the controller rendering, I am able to get the item but not the cacheable section.
    – SNew
    Commented Mar 6, 2021 at 15:26
  • Which version of Sitecore?
    – Mark Cassidy
    Commented Mar 6, 2021 at 16:25

1 Answer 1

1

Have you tried setting the Caching properties by using the RenderingItem class? I assume you are getting all rendering items or the current rendering in the controller and you can set the caching option on them

            var renderings = Context.Item.Visualization.GetRenderings(Context.Device, false);    
            foreach (var rendering in renderings)
            {
                rendering.RenderingItem.Caching.Cacheable = true;
                rendering.RenderingItem.Caching.VaryByData = true;
                rendering.Settings.Caching.Cacheable = true;
                rendering.Settings.Caching.VaryByData = true;
            }

Not sure if this is an option but you can also use powershell to loop through all rendering items and set Cacheable on them:

    Get-ChildItem -Path "master:\layout\Renderings" -Recurse | 
    Where-Object { $_.Cacheable -eq "0" } | 
    ForEach-Object { Write-Verbose "Enable caching on $($_.Name)"; $_.Cacheable = "1" }

I did a quick test on a vanilla instance and it seems that if you add the SecurityDisabler context it will work:

            using (new SecurityDisabler())
            {
                item.Editing.BeginEdit();
                item["Cacheable"] = "1";
                item["VaryByData"] = "1";
                item.Editing.EndEdit();
            }
4
  • I am not doing for the current item. Please see the above updated code and doing for all renderings at /sitecore/layout/Renderings
    – SNew
    Commented Mar 6, 2021 at 16:56
  • 1
    I updated my answer with powershell script in case you consider that approach Commented Mar 6, 2021 at 17:08
  • Cristi Thank you but we want the regular c# code as there is additional custom logic that needs to be implemented determining whether to enable or disable etc and it is not feasible easily via powershell. I am just showing a sample c# snippet that is failing in the out of box vanilla install
    – SNew
    Commented Mar 6, 2021 at 21:04
  • 1
    I updated the answer with the c# code that is working on my instance Commented Mar 7, 2021 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.