4

I have a rendering that is used on pages all over my site, and I had to add a new field to the Rendering Parameters it uses. I now need to update this field on a huge number of renderings, but only on renderings that exist within a particular placeholder, so I can't just apply it globally in standard values.

I've started writing a script to update the rendering parameters, and I think I've got the code to retrieve and modify the rendering parameters, but I'm not sure how to apply it back to the rendering and save it.

var testPage = db.GetItem("/sitecore/content/Home/Magazine/Issues/2009/August");
var renderings = testPage.Visualization.GetRenderings(Sitecore.Context.Device, true);

foreach (var rendering in renderings)
{
    var placeholderId = rendering.Placeholder;

    // check renderingitem id
    if (rendering.RenderingItem.ID == new Sitecore.Data.ID("{93F07F46-F356-419A-B937-A5EB2A9D63DD}"))
    {

        if (placeholderId == "/content-body/secondary-column-left")
        {

            var parameterString = rendering.RenderingItem.Parameters;
            var parameters = HttpUtility.ParseQueryString(parameterString);
            parameters["Display Subtitle"] = "1";
            parameterString = parameters.ToString();

            // HOW DO I PUT THIS BACK ON THE RENDERING NOW?
        }
    }
}
6

I've not completely test this using Sitecore PowerShell Extensions, but it should get you pretty close.

$item = Get-Item -Path "master" -ID "{961563FC-3445-4558-BF3A-06DF06BA6298}"
$renderingItemId = "{93F07F46-F356-419A-B937-A5EB2A9D63DD}"
$placeholderId = "/content-body/secondary-column-left"

# Accepts Hashtable, Ordered Dictionaries, etc.
$parameters = [ordered]@{"Display Subtitle"="1"}
Get-Rendering -Item $item -FinalLayout |
    Where-Object { $_.Placeholder -eq $placeholderId -and $_.ItemID -eq $renderingItemId } |
    Set-RenderingParameter -Parameter $parameters | 
    Set-Rendering -Item $item -FinalLayout
| improve this answer | |
5

Got the answer, here is the complete code. In my case I'm updating the Display Subtitle field but you can change this field or make the field name a variable if you want to:

public void SetRenderingParameterValue(
    Item item)
{
    using (new SecurityDisabler())
    {
        string defaultDeviceId = "{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}";
        string dynamicContentListingRenderingId = "{93F07F46-F356-419A-B937-A5EB2A9D63DD}";

        var layoutField = new LayoutField(item.Fields[Sitecore.FieldIDs.LayoutField]);
        var layoutDefinition = LayoutDefinition.Parse(layoutField.Value);
        var deviceDefinition = layoutDefinition.GetDevice(defaultDeviceId);
        foreach (RenderingDefinition rendering in deviceDefinition.Renderings)
        {
            var datasource = rendering.Datasource;
            var placeholderId = rendering.Placeholder;

            if (placeholderId == "/content-body/secondary-column-left")
            {
                if (rendering != null && rendering.ItemID == dynamicContentListingRenderingId)
                {
                    var parameters = HttpUtility.ParseQueryString(rendering.Parameters);
                    parameters.Remove("Display Subtitle");
                    parameters["Display Subtitle"] = "1";
                    var newParams = HttpUtility.UrlDecode(parameters.ToString());
                    rendering.Parameters = newParams;
                }
            }
        }
        item.Editing.BeginEdit();
        item[Sitecore.FieldIDs.LayoutField] = layoutDefinition.ToXml();
        item.Editing.EndEdit();

    }
}

The defaultDeviceId is the ID of /sitecore/layout/Devices/Default (or whatever Device your rendering is on)

dynamicContentListingRenderingId is the ID of my rendering item under /sitecore/layout/Renderings

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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