Skip to main content
1 of 2
James Walford
  • 1.1k
  • 1
  • 9
  • 24

Index isn't a property of the rendering definition object returned by Get-Rendering , even though you can use -Index as a parameter in Add-Rendering .

Using

$(Get-Rendering -Item $item -FinalLayout -Device $defaultDevice)[0] | Get-Member 

lists these:

TypeName: Sitecore.Layouts.RenderingDefinition

Name                MemberType     Definition
----                ----------     ----------
Equals              Method         bool Equals(System.Object obj)
GetHashCode         Method         int GetHashCode()
GetType             Method         type GetType()
LoadXml             Method         Sitecore.Xml.Serialization.XmlSerializable LoadXml(string xml), Sitecore.Xml.Serialization.XmlSerializable LoadXml(string xml, string xsltFilename)
SaveAsXml           Method         void SaveAsXml(string filename)
ToString            Method         string ToString()
ToXml               Method         string ToXml(), string ToXml(string xsltFilename)
Cachable            Property       string Cachable {get;set;}
ClearOnIndexUpdate  Property       string ClearOnIndexUpdate {get;set;}
Conditions          Property       string Conditions {get;set;}
Datasource          Property       string Datasource {get;set;}
DynamicProperties   Property       System.Xml.XmlAttribute[] DynamicProperties {get;set;}
ItemID              Property       string ItemID {get;set;}
MultiVariateTest    Property       string MultiVariateTest {get;set;}
Parameters          Property       string Parameters {get;set;}
PersonalizationTest Property       string PersonalizationTest {get;set;}
Placeholder         Property       string Placeholder {get;set;}
Rules               Property       System.Xml.Linq.XElement Rules {get;set;}
UniqueId            Property       string UniqueId {get;set;}
VaryByData          Property       string VaryByData {get;set;}
VaryByDevice        Property       string VaryByDevice {get;set;}
VaryByLogin         Property       string VaryByLogin {get;set;}
VaryByParameters    Property       string VaryByParameters {get;set;}
VaryByQueryString   Property       string VaryByQueryString {get;set;}
VaryByUser          Property       string VaryByUser {get;set;}
OwnerItemId         ScriptProperty System.Object OwnerItemId {get='{{20C2E00A-D359-420A-8E05-919CEE9EC686}}';}
OwnerItemPath       ScriptProperty System.Object OwnerItemPath {get="master:\content\Path\To\Your\Owner\Item";}

So you will need to run through the renderings returned with Get-Rendering and count them, until you reach the rendering before where you want to insert.

Incidentally, reading the renderings into an array in your script is unecessary:

$finalRenderings = @(Get-Rendering -Item $item -FinalLayout -Device $defaultDevice)

Powershell always returns a single object or a collection. You can replace this with:

$finalRenderings = Get-Rendering -Item $item -FinalLayout -Device $defaultDevice

You can also pipe that directly into a ForEach-Object and use the current object shorthand $_

$finalRenderings | ForEach-Object {
Write-Host "Rendering UniqueId: " $_.UniqueId
}

So if you know the ItemID of the rendering you want to insert after:

i=0
$matchID = "ID To Match"
$finalRenderings | ForEach-Object {
    i++
    if($_.ItemID -eq $matchID) {
        $MyIndex = i
        Break
    }
 }
James Walford
  • 1.1k
  • 1
  • 9
  • 24