SharePoint Pals
 | Sign In
Deleting SiteColumns and Remove From Content Types, Lists Using PowerShell in SharePoint 2013
by Sathish Nadarajan 23 Dec 2014
Author
SharePoint MVP
Visits   
Today  :   7     Total  :    18229

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.

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation