Advertisement
private775

PS: Archive SP List (MWI)

Jan 20th, 2016
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $w = get-spweb http://acme.com/sites/mwi
  2. $list = $w.Lists["JournalEntry"]
  3. $destList = $w.Lists["JurnalEntryArchive"]
  4. $archDate = get-date "Jan 1 2015"
  5.  
  6.  
  7. function CheckIfArchive($list, $id, $archDate) {
  8.    $ret = $false
  9.    $itm = $list.GetItemById($id)
  10.    $Submitted = -1
  11.    $Archived = -1
  12.    if($itm -ne $null){
  13.      $SubmissionDate = $itm["SubmissionDate"]
  14.      if($SubmissionDate -ne $null -and $archDate.CompareTo($SubmissionDate) -eq 1){
  15.        $Archived = $itm["Archived"]
  16.        if($Archived -ne $null -and $Archived -ne '' -and $Archived -eq 1){
  17.           $ret = $true
  18.        }
  19.        $Submitted = $itm["Submitted"]
  20.        if($Submitted -ne $null -and $Submitted -ne '' -and $Submitted -eq 1){
  21.           $ret = $true
  22.        }
  23.      }
  24.      
  25.    }
  26.    #Write-Host "$($id): $($SubmissionDate) $($Archived) $($Submitted): $($ret)"
  27.  
  28.    return $ret
  29. }
  30.  
  31.  
  32. function CopySpItem($sourceList, $destList, $id){
  33.  
  34. $destItem = $destList.Items.Add();
  35. $sourceItem = $sourceList.GetItemById($id)
  36. $sourceItem.Fields |%{
  37.     $f = $_;
  38.     if($f.ReadOnlyField -eq $false -and $f.InternalName -ne "Attachments" -and $sourceItem[$f.InternalName] -ne $null){
  39.         $destItem[$f.InternalName] = $sourceItem[$f.InternalName];
  40.     }
  41. }
  42. $destItem.Update();
  43. }
  44.  
  45.  
  46. $allIds = $list.Items|%{$_.ID}
  47.  
  48. foreach($id in $allIds) {
  49.    $doDelete = CheckIfArchive $list $id $archDate
  50.    if($doDelete) {
  51.       Write-Host -NoNewLine "x"
  52.       CopySpItem $list $destList $id
  53.    }
  54.    else { Write-Host -NoNewLine "." }
  55. }
  56.  
  57. foreach($id in $allIds) {
  58.    $doDelete = CheckIfArchive $list $id $archDate
  59.    if($doDelete) {
  60.       Write-Host -NoNewLine "x"
  61.       $itm = $list.GetItemById($id)
  62.       $itm.Delete()
  63.    }
  64.    else { Write-Host -NoNewLine "." }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement