Deleting SiteColumns and Remove From Content Types, Lists Using PowerShell in SharePoint 2013


Sathish Nadarajan
SharePoint MVP
Published On :   23 Dec 2014
Visit Count
Today :  5    Total :   20011
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


We saw how to create the Site Columns in last post. Now, let us see, how to remove them from the site through PowerShell.

Again, this will also have the same CSVs and PS1 file.

SiteUrl

SiteColumnInternalName

SiteColumnDisplayName

http://C4968397007:1001/sites/DemoAdmin

DemoTaxonomyField

Demo Taxonomy Field

http://C4968397007:1001/sites/DemoAdmin

DemoTextField

Demo TextField

http://C4968397007:1001/sites/DemoAdmin

DemoChoiceField

Demo ChoiceField

This CSV will have the Site and the Internal Name of the Site Column.

SiteUrl

SiteColumnInternalName

http://C4968397007:1001/sites/DemoAdmin

DemoTaxonomyField

http://C4968397007:1001/sites/DemoAdmin

DemoTextField

http://C4968397007:1001/sites/DemoAdmin

DemoChoiceField

Again, the above would be the input for the second portion. i.e., from the Content Types and the List, and from the Web Itself, the site column would be deleted.

The script as follows.

 $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
 $LogFile = ".\RemoveSiteColumnReferencePatch-$LogTime.rtf"
 
 cls
 
 $ErrorActionPreference = "SilentlyContinue"
 
 #start-transcript $logfile
 
 # Add SharePoint PowerShell Snapin
 
 
 if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
     Add-PSSnapin Microsoft.SharePoint.Powershell
 }
 
 $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
 Set-Location $scriptBase
 
  
 
 ######################Functions###################################################
 
 function RemoveSiteColumnReferenceInCT([string]$siteUrl, [string]$fieldName, [string]$SiteColumnDisplayName) 
 {
     Write-Host "Start removing field:" $fieldName -ForegroundColor DarkGreen
     $site = Get-SPSite $siteUrl
     $web = $site.RootWeb
 
     #Delete field from all content types
     foreach($ct in $web.ContentTypes) 
     {
         $fieldInUse = $ct.FieldLinks | Where {$_.Name -eq $fieldName }
         if($fieldInUse) 
         {
 	        write-host "The site column " $fieldname " is referenced in " $ct.name -fore yellow
             Write-Host "Remove Site column " $fieldname " which is referenced in CType: " $ct.Name -ForegroundColor DarkGreen
             $ct.FieldLinks.Delete($fieldName)
             $ct.Update()
 	        Write-Host "Site column " $fieldname " referenced in CType: " $ct.Name " is successfully removed............ Done !" -ForegroundColor Green
         }
     }
 
     $web.Dispose()
     $site.Dispose()
 }
 
 
 function RemoveSiteColumnListReference([string]$fieldName, [string]$SiteColumnInternalName) {
 
     get-spsite -limit all | Get-SPWeb -Limit all | ForEach-Object {
        
         $numberOfLists = $_.Lists.Count
         for($i=0; $i -lt $_.Lists.Count ; $i++) 
         {
             $list = $_.Lists[$i]
             
             if($list.Fields[$fieldName]) 
             {
                 $fieldInList = $list.Fields[$fieldName].InternalName
                 if($fieldInList -eq $SiteColumnInternalName) 
                 {
                     $_.URL + "," + $list.Fields[$fieldName] + "," + $fieldInList + "," + $list.Title | Out-File -Encoding Default -Append -FilePath $Output1;
                     Write-Host "Deleting Site column " $list.Fields[$fieldName] " from " $list.Title ” list on:” $_.URL -ForegroundColor cyan
 		            $fieldInList1 = $list.Fields.getfieldbyinternalname($SiteColumnInternalName)
                  
               		$fieldInList1.AllowDeletion = $true
                  
               		$fieldInList1.Delete()
 		            
                     Write-Host "Site column " $fieldName " referenced in list " $list.Title " is removed from " $_.URL " ........... Done ! "  -ForegroundColor Green
                  
               		$list.Update()
                 }
             }
         }
     }
     
 }
 
 
  
 
 function RemoveSiteColumn([string]$SiteUrl, [string]$fieldName)
 {
     $site = Get-SPSite $SiteUrl
 
     $web = $site.RootWeb
     $column = $web.Fields.getfieldbyinternalname($fieldName)
     if($Column)
     {
         write-host "Deleting site column " $fieldName " in site " $web.url -fore yellow
         $Column.Delete()
         write-host "site column " $column.Title " deleted successfully from the site " $web.url ".......... Done !" -fore blue
     }
 }
 
  
 
 ######################End of Functions###################################################
 
 
 ######################Calling Functions###################################################
 
 $SiteColumnDetails = $scriptBase + "\" + "01.SiteColumnDetails.csv"
 $RemoveSiteColumns = $scriptBase + "\" + "02.RemoveSiteColumns.csv"
 
 
 
 ##################Remove site column reference in content type############################
 
 import-csv $SiteColumnDetails | where {
     RemoveSiteColumnReferenceInCT $_.SiteUrl $_.SiteColumnInternalName $_.SiteColumnDisplayName
 }
  
 
 
 ##################Remove site column reference in List####################################
 
 import-csv $SiteColumnDetails | where {
     RemoveSiteColumnListReference $_.SiteColumnDisplayName $_.SiteColumnInternalName
     sleep(2)
 }
 
 
 ##################Remove site column#######################################################
 
 import-csv $RemoveSiteColumns | where {
     sleep(10)
     RemoveSiteColumn $_.SiteUrl $_.SiteColumnInternalName
 }
 
  
 #stop-transcript 
 
 
Download Script HERE 

Happy Coding.

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

KWizCom Scan