Skip to content

Instantly share code, notes, and snippets.

@asadrefai
Created June 2, 2015 09:56
Show Gist options
  • Select an option

  • Save asadrefai/d006034551d757ef2053 to your computer and use it in GitHub Desktop.

Select an option

Save asadrefai/d006034551d757ef2053 to your computer and use it in GitHub Desktop.
Reorder fields in a SharePoint content type using csom PowerShell
function Reorder-ContentTypeFields()
{
param(
[Parameter(Mandatory=$true)][string]$url,
[Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
[Parameter(Mandatory=$true)][string]$ContentTypeName
)
begin{
try
{
#get Client Object
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$Context.Credentials = $Credentials
#Load web object
$web = $Context.Web
$Context.Load($web)
$Context.ExecuteQuery()
}
catch
{
Write-Host "Error while getting context. Error -->> " + $_.Exception.Message -ForegroundColor Red
}
}
process{
try
{
$contentTypes = $Context.Site.RootWeb.ContentTypes
$Context.Load($contentTypes)
$Context.ExecuteQuery()
$contentType = $contentTypes | Where {$_.Name -eq $ContentTypeName}
$viewFields = New-Object System.Collections.Specialized.StringCollection
#Col1 = Internal Name of a field
$viewFields.Add("Col1")
$viewFields.Add("Col2")
$viewFields.Add("Col3")
$contentType.FieldLinks.Reorder($viewFields)
$contentType.Update($true)
$Context.Load($contentType)
$Context.ExecuteQuery()
Write-Host "Fields reordered successfully" -ForegroundColor Green
}
catch
{
Write-Host ("Error while reordering fields in contet type. Error -->> " + $_.Exception.Message) -ForegroundColor Red
}
}
end{
$Context.Dispose()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment