Skip to content

Instantly share code, notes, and snippets.

@OlafD
Created January 18, 2019 07:18
Show Gist options
  • Save OlafD/b144f275294422c1cb75237afce31177 to your computer and use it in GitHub Desktop.
Save OlafD/b144f275294422c1cb75237afce31177 to your computer and use it in GitHub Desktop.
Change the field order in a content type by PowerShell. As parameters, we need the name of the content type and the internal name of the field to reorder, and when the Mode parameter is set to Position the zero based number of the new position of the field. The script uses Load-CSOMProperties, a function implemented in the PBSPOPS module. When t…
param (
[Parameter(Mandatory=$true)]
[string]$ContentType,
[Parameter(Mandatory=$true)]
[string]$FieldName,
[ValidateSet("First", "Last", "Position")]
[string]$Mode,
[string]$NewPosition
)
function SetContentTypeFieldOrder
{
param (
[Parameter(Mandatory=$true)]
[string]$ContentType,
[Parameter(Mandatory=$true)]
[string]$FieldName,
[ValidateSet("First", "Last", "Position")]
[string]$Mode,
[int]$NewPosition
)
$ct = Get-PnPContentType -Identity $ContentType -ErrorAction SilentlyContinue
if ($ct -eq $null)
{
Write-Host -ForegroundColor Red "Content Type $ContentType could not be found."
}
else
{
Load-CSOMProperties -object $ct -propertyNames "FieldLinks"
Invoke-PnPQuery
$fieldLinksArray = GetFieldLinksAsArray -ContentType $ContentType
if ($NewPosition -gt $fieldLinksArray.Count)
{
Write-Host -ForegroundColor Red "New position $NewPosition is out of range for this content type."
}
else
{
switch ($Mode)
{
"First"
{
$reorderedFieldLinks = MoveToFirstPosition -Array $fieldLinksArray -Element $FieldName
$ct.FieldLinks.Reorder($reorderedFieldLinks)
$ct.Update($true)
Invoke-PnPQuery
}
"Last"
{
$reorderedFieldLinks = MoveToLastPosition -Array $fieldLinksArray -Element $FieldName
$ct.FieldLinks.Reorder($reorderedFieldLinks)
$ct.Update($true)
Invoke-PnPQuery
}
"Position"
{
if ($PSBoundParameters.ContainsKey("NewPosition") -eq $false)
{
Write-Host -ForegroundColor Red "Parameter NewPosition is mandatory for Mode 'Position'"
}
else
{
$reorderedFieldLinks = MoveElementToPosition -Array $fieldLinksArray -Element $FieldName -Position $NewPosition
$ct.FieldLinks.Reorder($reorderedFieldLinks)
$ct.Update($true)
Invoke-PnPQuery
}
}
}
}
}
}
function GetFieldLinksAsArray
{
param (
[Parameter(Mandatory=$true)]
[string]$ContentType
)
$ct = Get-PnPContentType -Identity $ContentType
Load-CSOMProperties -object $ct -propertyNames "FieldLinks"
Invoke-PnPQuery
$result = @()
$fieldLinks = $ct.FieldLinks
foreach ($fieldLink in $fieldLinks)
{
$result += $fieldLink.Name
}
return $result
}
function MoveElementToPosition
{
param (
[Parameter(Mandatory=$true)]
[string[]]$Array,
[Parameter(Mandatory=$true)]
[string]$Element,
[Parameter(Mandatory=$true)]
[int]$Position
)
$oldPosition = [Array]::IndexOf($Array, $Element)
if ($oldPosition -eq -1)
{
$result = $Array
}
else
{
if ($oldPosition -eq $Position)
{
$result = $Array
}
else
{
$result = @()
$haveSkipped = $false
for ($i = 0; $i -lt $Array.Count; $i++)
{
if ($i -eq $Position) # here we have to insert the element
{
if ($haveSkipped -eq $false)
{
$result += $Element
$result += $Array[$i]
}
else
{
$result += $Array[$i]
$result += $Element
}
}
else
{
if ($Array[$i] -eq $Element)
{
$haveSkipped = $true
}
else
{
$result += $Array[$i]
}
}
}
}
}
return $result
}
function MoveToFirstPosition
{
param (
[Parameter(Mandatory=$true)]
[string[]]$Array,
[Parameter(Mandatory=$true)]
[string]$Element
)
$reorderedArray = MoveElementToPosition -Array $Array -Element $Element -Position 0
return $reorderedArray
}
function MoveToLastPosition
{
param (
[Parameter(Mandatory=$true)]
[string[]]$Array,
[Parameter(Mandatory=$true)]
[string]$Element
)
$oldPosition = [Array]::IndexOf($Array, $Element)
if ($oldPosition -eq -1)
{
$result = $Array
}
else
{
$result = @()
for ($i = 0; $i -lt $Array.Count; $i++)
{
if ($Array[$i] -eq $Element)
{
}
else
{
$result += $Array[$i]
}
}
$result += $Element
}
return $result
}
if ($PSBoundParameters.ContainsKey("NewPosition") -eq $false)
{
SetContentTypeFieldOrder -ContentType $ContentType -FieldName $FieldName -Mode $Mode
}
else
{
SetContentTypeFieldOrder -ContentType $ContentType -FieldName $FieldName -Mode $Mode -NewPosition $NewPosition
}
Write-Host "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment