Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $host.Runspace.ThreadOptions = "ReuseThread"
- function Get-ListData
- {
- param ($sCSOMPath, $sSiteUrl,$ListTitle,$sUserName,$sPassword,$FileName)
- try
- {
- Write-Host "Getting all Items from the list $($ListTitle)"
- #Adding the Client OM Assemblies
- Add-Type -Path "C:\Microsoft.SharePoint.Client.dll"
- Add-Type -Path "C:\Microsoft.SharePoint.Client.Runtime.dll"
- #SPO Client Object Model Context
- $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)
- $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)
- #$spoCredentials = New-Object System.Net.NetworkCredential($sUserName, $sPassword)
- $spoCtx.Credentials = $spoCredentials
- $web = $spoCtx.Web
- $List = $web.Lists.GetByTitle($ListTitle);
- $caml="<View Scope='RecursiveAll'></View>"
- $cquery = New-Object Microsoft.SharePoint.Client.CamlQuery
- $cquery.ViewXml=$caml
- $listItems = $List.GetItems($cquery)
- $spoCtx.Load($List)
- $spoCtx.Load($listItems)
- $spoCtx.ExecuteQuery()
- $AllListItems = @()
- foreach ($listitem in $listItems)
- {
- $Title =""
- $CreatedBy = "System Account"
- $ModifiedBy = "System Account"
- #you can get custom fields of list also
- If([string]::IsNullOrEmpty($listitem.FieldValues["Title"]))
- {
- $Title = "No Title"
- }
- else
- {
- $Title =$listitem.FieldValues["Title"]
- }
- $FieldCreatedBy = [Microsoft.SharePoint.Client.FieldUserValue]$listitem.FieldValues["Author"]
- $CreatedBy = $FieldCreatedBy.LookupValue
- $FieldModifiedBy = [Microsoft.SharePoint.Client.FieldUserValue]$listitem.FieldValues["Editor"]
- $ModifiedBy = $FieldModifiedBy.LookupValue
- $AllListItems += New-Object -TypeName PSObject -Property @{
- ListTitle = $ListTitle
- ItemID=$listitem.ID
- ItemName=$Title
- CreatedBy = $CreatedBy
- ModifiedBy = $ModifiedBy
- CreatedDate = $listitem.FieldValues["Created"]
- ModifiedDate = $listitem.FieldValues["Modified"]
- } | Select ListTitle,ItemID,ItemName,CreatedBy,ModifiedBy,CreatedDate,ModifiedDate
- }
- $AllListItems| Export-CSV ($sCSOMPath+""+$FileName+".csv") -NoTypeInformation -Append #-Encoding UTF8
- $spoCtx.Dispose()
- Read-Host -Prompt "file created at $($sCSOMPath). Please presss any key to close this"
- }
- catch [System.Exception]
- {
- Write-Host -ForegroundColor Red $_.Exception.ToString()
- Read-Host -Prompt "Operation failed..! Press any key to close this and re run the script"
- }
- }
- $FileName = "All_ListItem_Collection"
- $sSiteUrl = "https://site collection url"
- $sUserName = "userid@domain.com"
- $sPassword = Read-Host -Prompt "Enter SharePoint online site password" -AsSecureString
- $ListTitle = "List Title"
- $scriptpath = $MyInvocation.MyCommand.Path
- $dir = Split-Path $scriptpath
- Get-ListData -sCSOMPath $dir -sSiteUrl $sSiteUrl -ListTitle $ListTitle -sUserName $sUserName -sPassword $sPassword -FileName $FileName
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement