Tuesday, 6 February 2018

SharePoint Content Type Columns Re-order with PowerShell

Multiple SharePoint List re-order using PowerShell


Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
Start-SPAssignment -Global #Start Assignment for disposal
$StartTime = Get-Date
$CurrentDirectory = (Get-Location).Path
$CSVLocation  = "$CurrentDirectory\CSVs"
$SiteCollList = Import-Csv -path "${CSVLocation}\SiteColl.csv"
foreach($SiteColl in $SiteCollList)
{
    $SPWeb = Get-SPWeb $SiteColl.Title
    $CTDetailsList = Import-Csv -path "${CSVLocation}\ColumnOrderDetails.csv"
    foreach($c in $CTDetailsList)
    {
        $ctname=$c.CTName
        $csvFileName=$c.ColumnOrderListName+".csv"
        Write-Host $ctname
        Write-Host $csvFileName
        $SPContentType = $SPWeb.ContentTypes[$c.CTName]
        $FieldList = Import-Csv -path "${CSVLocation}\$csvFileName"
        $OrderedFieldNames = @();
        foreach($Field in $FieldList)
        {
            $OrderedFieldNames += $Field.InternalName 
        }
        Write-Host $OrderedFieldNames
        if($SPContentType.ReadOnly)
        {
            $SPContentType.ReadOnly=$false
            $SPContentType.Update()
        }  
        $FieldToHide = $SPContentType.fields.GetFieldByInternalName("Title")
        $SPContentType.FieldLinks[$FieldToHide.Id].Hidden = $false
        $SPContentType.Update()
        write-Host "Making Title field as visible" -ForegroundColor Green
        write-Host $OrderedFieldNames
        $SPContentType.FieldLinks.Reorder($OrderedFieldNames)
        $SPContentType.Update($true)
        $FieldToHide = $SPContentType.fields.GetFieldByInternalName("Title")
        $SPContentType.FieldLinks[$FieldToHide.Id].Hidden = $true
        $SPContentType.Update()
        write-Host "Making Title field as Hidden" -ForegroundColor Green
        if(!$SPContentType.ReadOnly)
        {
            $SPContentType.ReadOnly=$true
            $SPContentType.Update()
        }
    }
    $SPWeb.Dispose()
}
Stop-SPAssignment -Global #Stop Assignment for disposal
$EndTime = Get-Date
Write-Host "Started:" $StartTime -ForegroundColor Green
Write-Host "Finished:" $EndTime -ForegroundColor Green

No comments:

Post a Comment