Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. $host.Runspace.ThreadOptions = "ReuseThread"
  2.  
  3. function Get-ListData
  4. {
  5. param ($sCSOMPath, $sSiteUrl,$ListTitle,$sUserName,$sPassword,$FileName)
  6. try
  7. {
  8. Write-Host "Getting all Items from the list $($ListTitle)"
  9.  
  10. #Adding the Client OM Assemblies
  11. Add-Type -Path "C:\Microsoft.SharePoint.Client.dll"
  12. Add-Type -Path "C:\Microsoft.SharePoint.Client.Runtime.dll"
  13.  
  14. #SPO Client Object Model Context
  15. $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)
  16. $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)
  17. #$spoCredentials = New-Object System.Net.NetworkCredential($sUserName, $sPassword)
  18. $spoCtx.Credentials = $spoCredentials
  19.  
  20. $web = $spoCtx.Web
  21. $List = $web.Lists.GetByTitle($ListTitle);
  22.  
  23. $caml="<View Scope='RecursiveAll'></View>"
  24. $cquery = New-Object Microsoft.SharePoint.Client.CamlQuery
  25. $cquery.ViewXml=$caml
  26. $listItems = $List.GetItems($cquery)
  27.  
  28. $spoCtx.Load($List)
  29. $spoCtx.Load($listItems)
  30. $spoCtx.ExecuteQuery()
  31.  
  32. $AllListItems = @()
  33.  
  34. foreach ($listitem in $listItems)
  35. {
  36. $Title =""
  37. $CreatedBy = "System Account"
  38. $ModifiedBy = "System Account"
  39.  
  40. #you can get custom fields of list also
  41. If([string]::IsNullOrEmpty($listitem.FieldValues["Title"]))
  42. {
  43. $Title = "No Title"
  44. }
  45. else
  46. {
  47. $Title =$listitem.FieldValues["Title"]
  48. }
  49.  
  50. $FieldCreatedBy = [Microsoft.SharePoint.Client.FieldUserValue]$listitem.FieldValues["Author"]
  51. $CreatedBy = $FieldCreatedBy.LookupValue
  52. $FieldModifiedBy = [Microsoft.SharePoint.Client.FieldUserValue]$listitem.FieldValues["Editor"]
  53. $ModifiedBy = $FieldModifiedBy.LookupValue
  54.  
  55. $AllListItems += New-Object -TypeName PSObject -Property @{
  56. ListTitle = $ListTitle
  57. ItemID=$listitem.ID
  58. ItemName=$Title
  59. CreatedBy = $CreatedBy
  60. ModifiedBy = $ModifiedBy
  61. CreatedDate = $listitem.FieldValues["Created"]
  62. ModifiedDate = $listitem.FieldValues["Modified"]
  63.  
  64. } | Select ListTitle,ItemID,ItemName,CreatedBy,ModifiedBy,CreatedDate,ModifiedDate
  65. }
  66. $AllListItems| Export-CSV ($sCSOMPath+""+$FileName+".csv") -NoTypeInformation -Append #-Encoding UTF8
  67.  
  68. $spoCtx.Dispose()
  69. Read-Host -Prompt "file created at $($sCSOMPath). Please presss any key to close this"
  70.  
  71.  
  72. }
  73. catch [System.Exception]
  74. {
  75. Write-Host -ForegroundColor Red $_.Exception.ToString()
  76. Read-Host -Prompt "Operation failed..! Press any key to close this and re run the script"
  77. }
  78. }
  79.  
  80.  
  81. $FileName = "All_ListItem_Collection"
  82. $sSiteUrl = "https://site collection url"
  83. $sUserName = "userid@domain.com"
  84. $sPassword = Read-Host -Prompt "Enter SharePoint online site password" -AsSecureString
  85. $ListTitle = "List Title"
  86.  
  87.  
  88.  
  89. $scriptpath = $MyInvocation.MyCommand.Path
  90. $dir = Split-Path $scriptpath
  91.  
  92.  
  93.  
  94. Get-ListData -sCSOMPath $dir -sSiteUrl $sSiteUrl -ListTitle $ListTitle -sUserName $sUserName -sPassword $sPassword -FileName $FileName
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement