Search This Blog

Wednesday, July 9, 2014

Powershell scripts to delete sharepoint List Items

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { Add-PSSnapin "Microsoft.SharePoint.PowerShell" } # "Enter the site URL instead http://serverurl" $SITEURL = "http://arwqa.grouphc.net/" $site = new-object Microsoft.SharePoint.SPSite ( $SITEURL ) $web = $site.OpenWeb() "Web is : " + $web.Title # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWAdjInvoice"]; $query=new-object Microsoft.SharePoint.SPQuery $query.ViewAttributes = "Scope='Recursive'" $items=$oList.GetItems($query) "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWAdjProductCode"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } #DEleting folders $query = New-Object Microsoft.SharePoint.SPQuery $camlQuery = 'Folder' $query.Query = $camlQuery $items = $oList.GetItems($query) Write-Host("DELETED FILE: " + $count) for ($intIndex = $items.Count - 1; $intIndex -gt -1; $intIndex--) { $items.Delete($intIndex); Write-Host("DELETED Folder: " + $intIndex) } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWCreditDebitOrder"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } #DEleting folders $query = New-Object Microsoft.SharePoint.SPQuery $camlQuery = 'Folder' $query.Query = $camlQuery $items = $oList.GetItems($query) Write-Host("DELETED FILE: " + $count) for ($intIndex = $items.Count - 1; $intIndex -gt -1; $intIndex--) { $items.Delete($intIndex); Write-Host("DELETED Folder: " + $intIndex) } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWDisputeInvoice"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWDrafts"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } #DEleting folders $query = New-Object Microsoft.SharePoint.SPQuery $camlQuery = 'Folder' $query.Query = $camlQuery $items = $oList.GetItems($query) Write-Host("DELETED FILE: " + $count) for ($intIndex = $items.Count - 1; $intIndex -gt -1; $intIndex--) { $items.Delete($intIndex); Write-Host("DELETED Folder: " + $intIndex) } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWErrorReason"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWInvoiceNewCustomer"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } #DEleting folders $query = New-Object Microsoft.SharePoint.SPQuery $camlQuery = 'Folder' $query.Query = $camlQuery $items = $oList.GetItems($query) Write-Host("DELETED FILE: " + $count) for ($intIndex = $items.Count - 1; $intIndex -gt -1; $intIndex--) { $items.Delete($intIndex); Write-Host("DELETED Folder: " + $intIndex) } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWInvoiceNewCustomerProductCode"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } #DEleting folders $query = New-Object Microsoft.SharePoint.SPQuery $camlQuery = 'Folder' $query.Query = $camlQuery $items = $oList.GetItems($query) Write-Host("DELETED FILE: " + $count) for ($intIndex = $items.Count - 1; $intIndex -gt -1; $intIndex--) { $items.Delete($intIndex); Write-Host("DELETED Folder: " + $intIndex) } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWJDEFailureTickets"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } #DEleting folders $query = New-Object Microsoft.SharePoint.SPQuery $camlQuery = 'Folder' $query.Query = $camlQuery $items = $oList.GetItems($query) Write-Host("DELETED FILE: " + $count) for ($intIndex = $items.Count - 1; $intIndex -gt -1; $intIndex--) { $items.Delete($intIndex); Write-Host("DELETED Folder: " + $intIndex) } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWK2Rules"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWNote"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } #DEleting folders $query = New-Object Microsoft.SharePoint.SPQuery $camlQuery = 'Folder' $query.Query = $camlQuery $items = $oList.GetItems($query) Write-Host("DELETED FILE: " + $count) for ($intIndex = $items.Count - 1; $intIndex -gt -1; $intIndex--) { $items.Delete($intIndex); Write-Host("DELETED Folder: " + $intIndex) } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWReasonCode"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } #DEleting folders $query = New-Object Microsoft.SharePoint.SPQuery $camlQuery = 'Folder' $query.Query = $camlQuery $items = $oList.GetItems($query) Write-Host("DELETED FILE: " + $count) for ($intIndex = $items.Count - 1; $intIndex -gt -1; $intIndex--) { $items.Delete($intIndex); Write-Host("DELETED Folder: " + $intIndex) } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWRecordCorrections"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWReinvoiceProductCode"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } #DEleting folders $query = New-Object Microsoft.SharePoint.SPQuery $camlQuery = 'Folder' $query.Query = $camlQuery $items = $oList.GetItems($query) Write-Host("DELETED FILE: " + $count) for ($intIndex = $items.Count - 1; $intIndex -gt -1; $intIndex--) { $items.Delete($intIndex); Write-Host("DELETED Folder: " + $intIndex) } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWReinvoiceSameCustomer"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWSalesinformation"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } #DEleting folders $query = New-Object Microsoft.SharePoint.SPQuery $camlQuery = 'Folder' $query.Query = $camlQuery $items = $oList.GetItems($query) Write-Host("DELETED FILE: " + $count) for ($intIndex = $items.Count - 1; $intIndex -gt -1; $intIndex--) { $items.Delete($intIndex); Write-Host("DELETED Folder: " + $intIndex) } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWStatusCode"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } #DEleting folders $query = New-Object Microsoft.SharePoint.SPQuery $camlQuery = 'Folder' $query.Query = $camlQuery $items = $oList.GetItems($query) Write-Host("DELETED FILE: " + $count) for ($intIndex = $items.Count - 1; $intIndex -gt -1; $intIndex--) { $items.Delete($intIndex); Write-Host("DELETED Folder: " + $intIndex) } # Enter name of the List below insted of LIST NAME $oList = $web.Lists["ARWTicketHistory"]; "List is :" + $oList.Title + " with item count " + $oList.ItemCount $collListItems = $oList.Items; $count = $collListItems.Count - 1 for($intIndex = $count; $intIndex -gt -1; $intIndex--) { "Deleting record: " + $intIndex $collListItems.Delete($intIndex); } #DEleting folders $query = New-Object Microsoft.SharePoint.SPQuery $camlQuery = 'Folder' $query.Query = $camlQuery $items = $oList.GetItems($query) Write-Host("DELETED FILE: " + $count) for ($intIndex = $items.Count - 1; $intIndex -gt -1; $intIndex--) { $items.Delete($intIndex); Write-Host("DELETED Folder: " + $intIndex) }

Powershell script to delete Sharepoint List Items with Folder

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

$site = Get-SPSite "http://arwqa.grouphc.net/"
$web = $site.rootweb
$list = $web.Lists["ARWTicket"]
$caml=""
#optional filter
#<Where><Eq><FieldRef Name=""ContentType"" /><Value Type=""Text"">Form</Value></Eq></Where>"

$query=new-object Microsoft.SharePoint.SPQuery
$query.ViewAttributes = "Scope='Recursive'"
$query.Query=$caml

$items=$list.GetItems($query)
Write-Host $items.Count


$count = $items.Count - 1
foreach ($item in $list.Items)
  {
 
    write-host "`rProcessing ID: $($item.ID) ($i of $batchSize)" -nonewline
   
   

        $listItem =$items.GetItemById($item.ID) #1187

        try
           {
                $IsRecord = [Microsoft.Office.RecordsManagement.RecordsRepository.Records]::IsRecord($listItem)
                if ($IsRecord -eq $true)
                {
               
                [Microsoft.Office.RecordsManagement.RecordsRepository.Records]::UndeclareItemAsRecord($listItem)
                    Write-Host "Success:Undeclared $($listItem.Title)"
                }
            }
        Catch [system.exception]
          {
                 Write-Host "Error:Item $($listItem.Title)"
          }
                 $listItem.Delete();

   
  }


#$items | % { $list.GetItemById($_.Id).Delete() }

#DEleting folders      
       
 $query =  New-Object Microsoft.SharePoint.SPQuery
$camlQuery = '<Where><Eq><FieldRef Name="ContentType" /><Value Type="Text">Folder</Value></Eq></Where>'
$query.Query = $camlQuery
$items = $list.GetItems($query)


 Write-Host("DELETED FILE: " + $count)

for ($intIndex = $items.Count - 1; $intIndex -gt -1; $intIndex--)
{
   $items.Delete($intIndex);
    Write-Host("DELETED FILE: " + $intIndex)
 
}



$web.Dispose()
$site.Dispose()

Sharepoint Long Running Process

  using (SPLongOperation longOperation = new SPLongOperation(this.Page))
                {
                    longOperation.LeadingHTML = Constants.MSG_LONG_OPERATION;
                    longOperation.TrailingHTML = "";
                    longOperation.Begin();
                    LH.ARW.Repository.DataModel.Ticket ticketValues = new LH.ARW.Repository.DataModel.Ticket();
                    BulkUpdates(ticketValues);
                    longOperation.EndScript("window.frameElement.commitPopup();");    
                }