0

We just moved from SharePoint 2007 to SharePoint Online. I want to retrieve the contents of a list, but the code below no longer works for SharePoint Online:

# Connect to Sharepoint Lists web service
$uri = "https://sharepoint2007site.com/_vti_bin/lists.asmx"
$service = New-WebServiceProxy -uri $uri –Credential $sharepoint_credential

# Set service url to SA portal
$service.url = "https://sharepoint2007site.com/teams/SysAdmin/_vti_bin/lists.asmx"

# Pull contents of server list
$listname = 'Listing'
$list = $service.GetListItems($listname, $null, $null, $null, 2500, $null, $null)

What is the SPO way of retrieving ListItems from a specified list?

1

The syntax to retrieve list items has changed. Please refer below code to get list items from SharePoint. To run this Open the SharePoint online powershell:

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  

$siteURL = ""  //insert your site url 
$userId = ""   //insert your user name, like abc@tenant.onmicrosoft.com
$pwd = Read-Host -Prompt "Enter password" -AsSecureString  
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
$ctx.credentials = $creds  
try{  
    $lists = $ctx.web.Lists  
    $list = $lists.GetByTitle("TestList")  //change to your custom list
    $listItems = $list.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())  
    $ctx.load($listItems)  

    $ctx.executeQuery()  
    foreach($listItem in $listItems)  
    {  
        Write-Host "ID - " $listItem["ID"] "Title - " $listItem["Title"]  
    }  
}  
catch{  
    write-host "$($_.Exception.Message)" -foregroundcolor red  
} 
  • I downloaded CSOM to get the dlls and ran the code. Returns this: Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (403) Forbidden." – A_B Oct 24 '16 at 14:30
  • did you enter site url and user name ? – Gautam Sheth Oct 24 '16 at 15:51
  • Figured it out...minor typo on my part – A_B Oct 24 '16 at 16:50
  • NB - the folder path for the specified DLLs maybe in [...]\16 instead of [...]\15 these days as new PowerShell/CSOM versions have come out. At least, I was able to run this code and I have the DLLs in a version \16\ folder, but nothing in the \15\ folder. – TylerH Jul 16 '19 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.