Delete a SharePoint list Field(Column) using PowerShell
Hello folks,
The following is a code snippet from PowerShell to completely remove or delete a field in a SharePoint list. The code uses the GUID to find the field (column). I tested this with SharePoint 2013.
Approch #1
#Set-ExecutionPolicy RemoteSigned
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
Get-PSSnapin –Registered
Add-PSSnapin Microsoft.SharePoint.PowerShell
#Variables
$SiteURL="xxxxxxxxx"
$ListName="News"
$ColumnName="WikiPlus"
#Get Internal Name of the columns
$web = Get-SPWeb $SiteURL
#Get the list
$list = $web.Lists.TryGetList($ListName)
if($List -ne $null)
{
#Get the column
$column = $list.Fields[$ColumnName]
if($column -ne $null)
{
#Reset column properties to allow delete
$column.Hidden = $false
$column.ReadOnlyField = $false
$column.AllowDeletion = $true
$column.Update()
#Delete the column from list
$list.Fields.Delete($column)
write-host "Column has been deleted!" -f Green
}
else
{
write-host "Specified column name not found!" -ForegroundColor Red
}
}
Approch #2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| #Source Help - http://dotnetfollower.com/wordpress/2012/07/sharepoint-how-to-delete-a-list-fieldcolumn-programmatically/ $list = $web .Lists.TryGetList( "My List Name" ) if ( $list -ne $null ) { foreach ( $column in $list .Fields){ if ( $column .Id -eq "[GUID]" ) { write-host -f green "Deleting column with Internal name as : " $column .InternalName if ( $column .ReadOnlyField) { $column .ReadOnlyField = $false ; $column .Update() } if ( $column .Hidden) { $column .Hidden = $false ; $column .Update() } if ( $column .AllowDeletion -eq $null -or ! $column .AllowDeletion.Value) { $column .AllowDeletion = $true $column .Update() } $column .Delete() $column .ParentList.Update() } } } else { write-host "list is null" } |
Comments
Post a Comment