Sitecore SXA updating NameValueList in (Rendering variant Data Attributes) using powershell and programmatically

 We have requirement to bulk update VariantSection template "Data Attribute" field's one property "alt" or "data-alt" with some custom value.

I have given solution using powershell and C#

Powershell way 

 $items = Get-ChildItem -Path "master:\sitecore\content\Shared\Presentation\Rendering Variants" -Recurse | Where-Object { $_.TemplateId -eq "{ACA5C9A3-CDA0-49B6-893F-1D4A7CB595B0}" }

foreach($item in $items) 

{

    $isAlt=$false

    $nameValues = [System.Web.HttpUtility]::ParseQueryString($item.Fields["DataAttributes"])

    Write-Host $nameValues

    foreach($key in $nameValues.AllKeys) 

    {

        if ($key -eq "alt" -Or $key -eq "data-alt")

        {

            $isAlt=$true

            $nameValues[$key] = "sample value"

            Write-Host $key= $nameValues[$key]

        }

    }

    if($isAlt)

    {

        $item.Editing.BeginEdit()        $item.Fields["DataAttributes"].Value=Sitecore.StringUtil]::NameValuesToString($nameValues,"&")

        $item.Editing.EndEdit()

        Write-Host $item.Fields["DataAttributes"]        

    }

}


C# way

private  void UpdateAttribute(Sitecore.Data.Items.Item[] links)

        {

            if (links != null)

            {

                foreach (Sitecore.Data.Items.Item nv in links)

                {

                    using (new SecurityDisabler())

                    {

                        string _attr = nv["DataAttributes"];

                        if (!string.IsNullOrEmpty(_attr))

                        {

                            NameValueCollection nameValueCollection = Sitecore.Web.WebUtil.ParseUrlParameters(_attr);

                            if (nameValueCollection.AllKeys.Any(p => p == "alt" ||p== "data-alt" ))

                            {

                                var data = new NameValueCollection();

                                foreach (string nvi in nameValueCollection.AllKeys)

                                {

                                    if (nvi == "alt" || nvi== "data-alt")

                                    {                                

  

                                        data.Add(nvi, Uri.EscapeDataString("sample value"));

                                    }

                                    else

                                        data.Add(nvi, Uri.EscapeDataString(nameValueCollection[nvi]));

                                }

                                nv.Editing.BeginEdit();

                                var result = StringUtil.NameValuesToString(data, "&");

                                result=result.Replace("\r\n", String.Empty).Replace("\r", String.Empty).Replace("\n", String.Empty).Replace("\t", String.Empty).Replace('\xA0', '\x20');

                                nv["DataAttributes"] = result;

                                nv.Editing.EndEdit();

                               

                            }

                        }

                    }

                } 

            }          

        }


Comments

Popular posts from this blog

Custom Item Url and resolving the item in Sitecore - Buckets

Fixing Sitecore Buckets folder path - Items created after 12 AM server time zone

Sitecore Search - API Crawler with Edge Pagination