Adding Rendering to Item presentation using Powershell Scripting.

Scenario: Assume a new rendering is created and we need to assign this rendering to multiple items in the Sitecore content tree. while this can be done manually it will be time consuming and boring task for content authors. Again Powershell come to the rescue.

Get Item where rendring needs to be assigned.
$item=Get-Item -Path “master:” -ID “{3512740C-8965-4E40-A082-0F528A638111}”
Get rendering which needs to be assigned on the above item
$RenderingName = Get-Item -Path “master:” -ID {209B30A5-495C-4AF0-A670-8287E78107DE}
write-host “adding rendering…”
Add-Rendering -Item $Item -PlaceHolder “content123” -DataSource “” -Instance (New-Rendering -Item $RenderingName)
write-host “add rendering completed…”

Note: Here you can provide the Placeholder and Datasource too based on the requirement.

The same Process can be automated with multiple items and multiple renderings where you are reading the item IDs from a csv file and assigning the renderings in a foreach loop.

Calling Sitecore Controller rendering dynamically on the view(cshtml) file and passing rendering parameters

We can call the Controller renderings already created on a view file. In some scenarios we can not assign the rendering on the item (for ex wildcard scenarios where we need this rendering on specific item not all the items)

@Html.Sitecore().Rendering(itemid, new { DataSource = itmds.ID})

wher Datasource is

Item itmds = Sitecore.Context.Database.GetItem(“Path or Id or some Datasource field”);

    

Passing Rendering Parameters dynamically

Lets assume we are passing orientation and Background color with the Datasource where Orientation is passed as string.

Item itorient = Sitecore.Context.Database.GetItem( “Path or Id or some Datasource field” );
Item itbccolor = Sitecore.Context.Database.GetItem(” Path or Id or some Datasource field );

string Orient = string.Format(“Orientation={0}&Background Color={1}”, itorient.Name, itbccolor.ID);

Get All items based on specific template under bucket folder which got updated x days ago.

Scenario: Let assume that due to some wrong migration from external system lot of item fields got updated under Sitecore bucket folder. If we need to fix this kind of scenario we can use the Powershell scripting.

$allItems = Get-ChildItem “master:/sitecore/content/products” -recurse | where-object {$_.TemplateID -eq “{70E9D00C-2A10-41EE-BE6D-8441FD4C8C79}” -and $_.__Updated -gt [datetime]::Now.AddDays(-10) }

How to update one of the Fields.

foreach($item in $allItems){

write-host $item.ID “;” $item.Name
$item.Editing.BeginEdit()
$item.Fields[“FieldtoUpdate”].Value = “CorrectValue”
$item.Editing.EndEdit()

}