Search code examples
powershellsharepoint-onlinecsom

I'm trying to pull a list of items from sharepoint online using powershell csom


I can get the list data - but it's all of the list history and not just the most current version for the items. I don't see a version ID field to filter off older versions of the item. Is there a way to do this with csom and powershell?

Here is my current code - I just need a way to filter to active items.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Variables for Processing
$SiteUrl = "XYZ"
$ListName="ABC"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $credentials
   
#Get the List
$List = $Context.web.Lists.GetByTitle($ListName)
 
#sharepoint online get list items powershell
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Context.Load($ListItems)
$Context.ExecuteQuery()      
 
write-host "Total Number of List Items found:"$ListItems.Count

#Loop through each item
$ListItems | ForEach-Object {
    If ($_["Status"]  = "Returned") {
        #Get the Title field value
        write-host $_.ID
        write-host $_["Title"]
    }
}

Solution

  • Please run the below PowerShell CSOM script as an admin:

    #Load SharePoint CSOM Assemblies
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
       
    #Variables for Processing
    $SiteUrl = "https://****.sharepoint.com/sites/sitename"
    $ListName="ABC"
    
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
      
    #Set up the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $credentials
       
    #Get the List
    $List = $Context.web.Lists.GetByTitle($ListName)
     
    #sharepoint online get list items powershell
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "<View><ViewFields><FieldRef Name='ID'/><FieldRef Name='Title'/></ViewFields><Query><Where><Eq><FieldRef Name='Status'/><Value Type='Choice'>Returned</Value></Eq></Where></Query></View>"
    
    $ListItems = $List.GetItems($Query)
    $Context.Load($ListItems)
    $Context.ExecuteQuery()
     
    Write-host "Total Number of Items:"$ListItems.count
     
    #Loop through each List Item
    $ListItems | ForEach-Object {
         #Get the Title field value
         write-host $_.ID
         write-host $_["Title"]  
         Write-host " ************* "
    }
    

    enter image description here

    enter image description here

    ========================== Updated Answer =========================

    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "<View><ViewFields><FieldRef Name='ID'/><FieldRef Name='Title'/></ViewFields><Query><Where><Eq><FieldRef Name='Status'/><Value Type='Choice'>Returned</Value></Eq></Where></Query></View>"
    

    Query the items in the current list whose [Status] column value is equal to "Returned"

    enter image description here

    enter image description here