Advertisement
private775

[PS] Get all SP List items into array of hash tables

Mar 27th, 2019
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-PSSnapin Microsoft.SharePoint.PowerShell
  2. # IDS: 7,53
  3. # $ids = 7,53
  4. $webUrl = "http://acme.com/"
  5. $listName = "Long List"
  6.  
  7. # show all fields
  8. $w = get-spweb $webUrl
  9. $l = $w.Lists[$listName]
  10.  
  11. [System.Collections.Generic.List[string]]$fldNames = $l.Fields|?{ -not $_.FromBaseType}| % {$_.InternalName}
  12.  
  13.  
  14.  
  15.  
  16. $ids = $l.Items|%{$_.ID}# |Select-Object -First 15
  17.  
  18. $arr = [System.Collections.ArrayList]@()
  19.  
  20. Write-Host -NoNewline "Working "
  21.  
  22. [int]$cnt = 1
  23.  
  24. foreach($id in $ids){
  25.     $item = $l.GetItemById($id)
  26.     $ht = @{}
  27.     $ht["ID"] = $item.ID
  28.     $ht["Title"] = $item.Title
  29.     $ht["Created"] = $item["Created"]
  30.  
  31.     foreach($fldName in $fldNames){
  32.       $fld = $l.Fields.GetFieldByInternalName($fldName)
  33.       $val = $item[$fld.Id]
  34.       $valStr = ""
  35.       if($val -ne $null){
  36.         $valStr = $val.ToString()
  37.       }
  38.       $ht[$fld.Title] = $valStr
  39.     }
  40.     $arr.Add($ht)|Out-Null
  41.     $cnt++
  42.     if(($cnt % 100) -eq 0) {
  43.         Write-Host -NoNewline "."
  44.     }
  45. }
  46.  
  47.  
  48. Write-Host " done"
  49.  
  50. Write-Host "Found $($arr.Count) items, stores in variable `$arr"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement