Remove Site Columns From Libraries One Subsite

RemoveSiteColumnsFromLibrariesOneSubsite.ps1

# RemoveSiteColumnsFromLibrariesOneSubsite: Removes the site columns identified in the input list (in the XML file)
# from all lists and libraries in one single subsite.
# Syntax example:
# .\RemoveSiteColumnsFromLibrariesOneSubsite.ps1 http://bmphub.int/projects/itduc "D:\SiteColumns.xml"
#=========Parameter Section=============
param(
    [string] $subsiteURL = $(Throw "-- You must specify the URL of a subsite 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 "RemoveSiteColumnsFromLibrariesOneSubsite: Processing subsite: " $subsiteURL -BackgroundColor Yellow -ForegroundColor Black
$siteScope = Start-SPAssignment
$web = get-spweb $subsiteURL
$site = $web.site
# get the site column XML from the input XML file
$XmlFileInput = [xml](Get-Content($xmlFilePath))
# build list of objects to save the list titles and column names to delete
$array = @()
# loop over lists in the subsite
foreach ($list in $web.lists)
{
    # only process Document Libraries: skip lists
    if ($list.BaseType -ne 'DocumentLibrary') {continue}
    write-host "Checking library '" $list.Title "' in subsite '" $web.Url "'" -ForegroundColor Gray 
    # loop over site columns listed in the input XML file
    foreach( $sitecolumn in $XmlFileInput.SiteColumns.SiteColumn)
    {
        $siteColumnToRemove = $sitecolumn.Name
        if ($list.Fields[$siteColumnToRemove]) # list does have this content type
        {
            if ($list.Fields[$siteColumnToRemove].Hidden) 
            {          
                write-host "    Site Column '" $siteColumnToRemove "' is HIDDEN in '" $list.Title "' in site '" $web.Url "'" -ForegroundColor Black 
            }
           
            # save name of list title and site column to remove from this list in array
            $objListAndCol = New-Object System.Object
            $objListAndCol | Add-Member -type NoteProperty -name ListTitle -value $list.Title
            $objListAndCol | Add-Member -type NoteProperty -name ColName -value $siteColumnToRemove
            $array += $objListAndCol
       
        }
    }
}
   
# now delete all the site columns from the lists
foreach($objListAndCol in $array)
{
    $listTitle = $objListAndCol.ListTitle
    $colName = $objListAndCol.ColName
    try
    {
        $list2 = $web.lists[$listTitle]
       
        if ($list2.Fields[$colName])
        {           
            if ($list2.Fields[$colName].Hidden)
            {
                $colToDelete = $list2.Fields[$colName] 
                $colToDelete.AllowDeletion = $true
                $colToDelete.Update()
                $list3 = $web.lists[$listTitle]
                $list3.Fields[$colName].Delete()
                $list3.Update()
            }
            else
            {
                $list2.Fields[$colName].Delete()
                $list2.Update()
            }
            write-host "    Deleted column '" $colName "' from '" $listTitle "' in site '" $web.Url "'" -BackgroundColor Yellow -ForegroundColor Black
        }
    }
    Catch
    {
        write-host "Exception      :" $_.exception.message -ForegroundColor Red
        write-host "Deleting column:" $colName -ForegroundColor Red
        write-host "From List      :" $listTitle -ForegroundColor Red
        write-host "in Web         :" $web.url -ForegroundColor Red
    }
}               
$site.dispose
Stop-SPAssignment $siteScope
Write-host "RemoveSiteColumnsFromLibrariesOneSubsite: Done."

No comments:

Post a Comment