Sunday, 23 October 2011

Powershell Script To Remove the columns "Barcode", "Barcode Value", "Exempt from Policy" when removing IRM policy in a list in Sharepoint 2007 and Sharepoint 2010

Hi,

Recently someone expressed his issue of unable to remove the columns "Barcode", "Barcode Value" and "Exempt from Policy" from the list even after the policy is removed. These columns can be simply removed from the view by going into the view settings and checking these columns. However the requester expressed his concerns as why these columns where still there and why there were not removed when there the policy was removed. Hence to solve this I created the following script. It takes the site URL and list name as input values.

There was no option to delete these columns through the UI.
When initially I tried removing them from script, I got this:

Exception calling "Delete" with "0" argument(s): "Operation is not valid due to the current state of the object."

I modified my script and now it is working fine. This has been tested in both Sharepoint 2007 and Sharepoint 2010



If you are executing this make sure you have removed the policy otherwise you will face some other issues

param([switch]$help)

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

function GetHelp() {


$HelpText = @"

DESCRIPTION:
This script will remove the columns "Barcode", "Barcode Value" and "Exempt from Policy" from a list. These columns are added when Barcodes are enabled using IRM

policy. Even after the policy is withdrwan these columns stay in the list of columns under view settings.
"@
$HelpText

}

function RahulDeleteBarcodeIRMPolicyColumns() {

write-host "This script will remove barcode related columns from your list. Before executing this tool make sure that you have removed the policy otherwise it

will take you to some other issues"
        write-host "Please enter your site url"
        $siteURL = read-host
        write-host "Please enter your List Name"
        $listName = read-host
        $site = New-Object Microsoft.SharePoint.SPSite($siteURL)
        $Web = $site.OpenWeb()
        $web.AllowUnsafeUpdates = $true
        $listSource = $web.Lists[$listName]
        $columnNames = @("Barcode", "Barcode Value", "Exempt from Policy")
        for ($i = 0; $i -lt $columnNames.Length; $i++)
        {
           $fieldtoBedeleted = $listSource.Fields[$columnNames[$i]]
           $fieldtoBedeleted.AllowDeletion = $true
           $fieldtoBedeleted.Sealed = $false
           $fieldtoBedeleted.Delete()
                   
        }
        $web.AllowUnsafeUpdates = $false
        write-host "The columns are deleted successfully"
$site.Dispose()
$web.Dispose()
}



if($help) { GetHelp; Continue }
else { RahulDeleteBarcodeIRMPolicyColumns }


I hope this will help you out.

Thanks,
Rahul Rashu

No comments:

Post a Comment