-->

08/05/2014

Bulk "Undelcare" and "Send To" in SharePoint

Recently i have a small issue with Information management policies in one of the sharepoint environments.
So as a result i have nearly 10,000 records which need to be moved across to another Site collection.

In order to undeclare a record, i need to go to complaince details of each document and click that "Undeclare record" button.

Now coming to "Send To" functionality, i need to open context menu of each item and select the specific "Send to" connection.

Now i cannot do this for 10,000 documents. So this is why i love Powershell.

The below script have take care of both of the tasks in one shot.

Start-Transcript -Path .\Incident.log
$webapp= Get-SPWebApplication "WebApplicationUrl here . . ."
$web=Get-SPWeb "SiteCollection url here . . . "
$list=$web.Lists["Documents"]
$conn=$webapp.OfficialFileHosts | Where-Object {$_.OfficialFileName -eq "Send To connection name goes here . . ."}
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewAttributes = "Scope='Recursive'";
$spQuery.RowLimit = 4000
$caml = '<Where><IsNotNull><FieldRef Name="_vti_ItemDeclaredRecord" /></IsNotNull></Where><OrderBy Override="TRUE"><FieldRef Name="ID"/></OrderBy>' 
$spQuery.Query = $caml 
Write-Host "Querying 4000 items at a time." -ForegroundColor Yellow
do
{
    Write-Host "Going for current 4000 items" -ForegroundColor Yellow
    $listItems = $list.GetItems($spQuery)
    $spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
    

    if($listItems -ne $null)
{
Write-Host "Found records in current 4000" -ForegroundColor Yellow
foreach($item in $listItems)
{
try
{
[Microsoft.Office.RecordsManagement.RecordsRepository.Records]::UndeclareItemAsRecord($item);

$recordItem = $list.GetItemById($item.ID);
if($recordItem.File -ne $null)
   {

$file=$recordItem.File;
$outputResult= "";
       $file.SendToOfficialFile("Content type name goes here . . .",$conn,"",[Microsoft.SharePoint.SPOfficialFileSubmissionMode]::ExpirationPolicy,[ref]$outputResult);

   }

catch [System.Exception]
{
Write-Host "Error while moving a document . . . Continuing Anyways" -ForegroundColor Red
continue;
}
}
}
else
{
Write-Host "Found no records in current 2000" -ForegroundColor Yellow
}
    
}
while ($spQuery.ListItemCollectionPosition -ne $null)
Write-Host "Finished !" -ForegroundColor Yellow
Stop-Transcript

The above script does the following:
    1. It will take care of threshold (5000) issue into consideration, thus processing 4000 items at a time.
    2. First acquire the send to connection.
    3. Undeclare each records, and then send it to the specific send to connection.
    4. Creates a log file and logs the transactions into it (Incident.log)
  5. If there is any exception happened with an item, it will log the error and then continues with next item.

Here is how the execution looks like :


It took 15 minutes to complete the task but, i am able to undeclare and move 10,000 records with a single click.

No comments:

Post a Comment