1

With the following code, I am getting this error in powershell: I could not find out whats wrong here? I am using recycle() method because in next step, i need to restore items from recycle bin.

Method invocation failed because [Microsoft.SharePoint.SPContentTypeId] doesn't contain a method named 'recycle'.
At C:\Users\administrator\Desktop\Test.ps1:76 char:9
+         $DeletedItem = $ListItems[$i].recycle()

Function Get-SitesWithNewsPageCT
{
    $Global:site | Get-SPWeb -Limit all | ForEach-Object {

     $PagesLibrary = $_.Lists[$Global:lookForList]

    if($PagesLibrary -ne $null)
    {

        if($PagesLibrary.ContentTypes[$Global:CT_To_Add_Remove])
        {
            $Global:site_To_AddCT = $_.Url
            $PagesLibrary.AllowDeletion = $true
            $NewsPageCT = $PagesLibrary.ContentTypes[$Global:CT_To_Add_Remove]

                if($NewsPageCT -ne $null)
                {               

                    $iD = $NewsPageCT | Select Id
                    $idString = $iD.Id.ToString()
                    $ListItems = $PagesLibrary.Items | Where-Object { $_.ContentTypeId -eq $idString }

                    Remove-NewsPageCT $ListItems, $NewsPageCT, $PagesLibrary, $idString
                }
            $Global:TotalSitesWithNewsPageCT++
        }

    }
    $Global:Total_Sites
    }

    Write-Host "Total Sites are: " $Global:Total_Sites
    Write-Host "Total Sites to Remove/Add NewsPage content type: " $Global:TotalSitesWithNewsPageCT
}

Function Remove-NewsPageCT
{
    param($ListItems,$NewsPageCT,$PagesLibrary,$idString)

    [System.Collections.ArrayList]$DeletedItemsIDs = @()
    [System.Collections.ArrayList]$DeletedItems = @()
    $count = 0

    if($ListItems.Count -gt 0)
    {
        for($i = $ListItems.Count-1; $i -ge 0; $i--)
        {
            $DeletedItemsIDs.Add($ListItems[$i].ID)
            $DeletedItem = $ListItems[$i].recycle()       
            $DeletedItems.add($DeletedItem)
            $count++             
        }
    }

    Start-Sleep -Seconds 5

    $NewsPageCT.ReadOnly = $false
    $NewsPageCT.Update()
    $PagesLibrary.ContentTypes.Delete($idString)
    $PagesLibrary.Update()

    Start-Sleep -Seconds 5

    Add-NewsPageCT $PagesLibrary $DeletedItemsIDs $DeletedItems $count

}
  • Check what items you've got in the $DeletedItem array. Seems there might be something else mixed between list items. – Paul Strupeikis Feb 25 '16 at 14:12
  • sounds like a $ListItems problem. perhaps you could show how you're populating that – Tiago Duarte Feb 25 '16 at 14:27
  • @TiagoDuarte: I have updated my question – SPBeginer Feb 25 '16 at 14:31
  • apparently your ListItems array doesnt contain items of type SPListItem as it should. could either be some code specific issue, or memory issue. try closing your IDE if you use any, close any shells and run the script again form a clean console – Tiago Duarte Feb 25 '16 at 14:58
  • I have already closed the IDE and started Powershell again, but still same error. – SPBeginer Feb 25 '16 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.