Remove Site Columns From Site Collection

RemoveSiteColumnsFromSiteCollection.ps1

# RemoveSiteColumnsFromSiteCollection: Remove all listed Site Columns from the Site Collumn Gallery of a site colleciton.
# This assumes all Content Types that use the given Site Colunns have alread been removed.
# Syntax example:
# .\RemoveSiteColumnsFromSiteCollection.ps1 http://bmphub.int/projects "D:\Scripts\SiteColumns.xml"
#=========Parameter Section=============
param(
    [string] $siteCollectionURL = $(Throw "-- You must specify the URL of a site collection as parameter 1."), #required parameter
 [string] $xmlFilePath = $(Throw "-- You must specify the pathname of an XML file as parameter 2.") #required parameter
)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"
Write-host "RemoveSiteColumnsFromSiteCollection: Processing Site Colleciton: " $siteCollectionURL -BackgroundColor Yellow -ForegroundColor Black
$siteScope = Start-SPAssignment
$site = get-spsite $siteCollectionURL
# get the site column XML from the input XML file
$XmlFileInput = [xml](Get-Content($xmlFilePath))
# no need to loop over allwebs - just process the root site    
$web = $site.rootweb
# loop over site columns listed in the input XML file
foreach( $sitecolumn in $XmlFileInput.SiteColumns.SiteColumn)
{
    $siteColumnToRemove = $sitecolumn.Name
   
    # write-host "  Checking for Site Column '" $siteColumnToRemove "' in rootweb '" $web "'" -ForegroundColor Gray
    $sc = $web.Fields[$siteColumnToRemove]
    if ($sc)
    {
        # write-host "    Deleting Site Column '" $sc.Title "' from '" $web "'" -ForegroundColor Gray
        try
        {
            $web.Fields.Delete($sc)
            write-host "    Deleted Site Column '" $siteColumnToRemove "' from '" $web.url "'" -ForegroundColor Black
        }
        Catch
        {
            Write-host "Exception   :" $_.exception.message -ForegroundColor red
            Write-host "Web         :" $Web.url -ForegroundColor red
            Write-host "Site Column :" $siteColumnToRemove -ForegroundColor red
            Write-host "Field ID    :" $sc.Id -ForegroundColor red
        }
    }
}
$web.Update()
$site.dispose
Stop-SPAssignment $siteScope
Write-host "RemoveSiteColumnsFromSiteCollection: Done."

No comments:

Post a Comment