Created
May 11, 2015 15:13
-
-
Save asadrefai/de0aa10bfe16ce380420 to your computer and use it in GitHub Desktop.
PowerShell to change the display name of a field in a SharePoint List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Change-DisplayNameOfField() | |
{ | |
param( | |
[Parameter(Mandatory=$true)][string]$url, | |
[Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials, | |
[Parameter(Mandatory=$true)][string]$listName, | |
[Parameter(Mandatory=$true)][string]$fieldName, | |
[Parameter(Mandatory=$true)][string]$displayName | |
) | |
begin{ | |
try | |
{ | |
#get Client Object | |
$context = New-Object Microsoft.SharePoint.Client.ClientContext($url) | |
$context.Credentials = $credentials | |
#Retrieve List | |
$list = $context.Web.Lists.GetByTitle($listName) | |
$context.Load($list) | |
$context.ExecuteQuery() | |
} | |
catch | |
{ | |
Write-Host "Error while getting context. Error -->> " + $_.Exception.Message -ForegroundColor Red | |
} | |
} | |
process{ | |
try | |
{ | |
$field = $list.Fields.GetByInternalNameOrTitle($fieldName) | |
$field.Title = $displayName | |
$field.Update() | |
$list.Update() | |
$context.Load($field) | |
$context.Load($list) | |
$context.ExecuteQuery() | |
Write-Host "Display name changed successfully" -ForegroundColor Green | |
} | |
catch | |
{ | |
Write-Host ("Error while changing display name for a Field. 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