Connect SharPoint Online WebService

Tan Phat Huynh 41 Reputation points
2021-08-03T20:00:43.527+00:00

Hello, I am trying to modify the below to authenticate to SharePoint Online instead of OnPrem server? How can I achieve this? Thank you.

Connect to web service

$uri = "https://test.sharepoint.com/sites/teamsites/_vti_bin/lists.asmx"
$service = New-WebServiceProxy -uri $uri -Namespace SpWs -UseDefaultCredential
$xmlDoc = new-object System.Xml.XmlDocument
$listName = "TestList"
$viewFields = $xmlDoc.CreateElement("ViewFields")
$query = $xmlDoc.CreateElement("Query")
$list = $service.GetListItems($listname,$null, $null, $viewfields,$null,$null,$null)

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,628 questions
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 37,626 Reputation points Microsoft Vendor
    2021-08-10T09:40:35.743+00:00

    Hi @Tan Phat Huynh ,
    We can try retrieve all column name and get items by title.Following code for sample:

     $siteURL = "site url"    
     $userId = "abc@tenant.onmicrosoft.com"     
     $pwd = Read-Host -Prompt "123456" -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("TestList01")    
         $listItems = $list.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())    
         $ctx.load($listItems)    
         $ctx.load($list.Fields)   
          
         $ctx.executeQuery()    
         foreach($Field in $list.Fields){    
            foreach($listItem in $listItems){    
              Write-Host $Field.Title " - " $listItem[$Field.Title]   
            }  
         }    
     }    
     catch{    
         write-host "$($_.Exception.Message)" -foregroundcolor red    
     }   
    

1 additional answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 37,626 Reputation points Microsoft Vendor
    2021-08-05T06:51:29.34+00:00

    Hi @Tan Phat Huynh ,
    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:

    $siteURL = "site url"    
    $userId = "abc@tenant.onmicrosoft.com"     
    $pwd = Read-Host -Prompt "123456" -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("TestList01")    
        $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    
    }   
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.