question

TanPhatHuynh-4230 avatar image
0 Votes"
TanPhatHuynh-4230 asked TanPhatHuynh-4230 commented

Connect SharPoint Online WebService

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)

office-sharepoint-server-development
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @TanPhatHuynh-4230 , based on your needs we modify the tag for you. Thanks for your support and understanding.

0 Votes 0 ·
RaytheonXie-MSFT avatar image
0 Votes"
RaytheonXie-MSFT answered TanPhatHuynh-4230 commented

Hi @TanPhatHuynh-4230 ,
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  
  } 


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks @RaytheonXie-MSFT. Seems to work retrieving columns but most fields returned blank such as Modified By. What I am really after is to display the Modified By which is displayed as their "Account" attribute not "Name" attribute which are the two options for SPO Person field. Is it possible?



eg output:

Modified By -
Modified By -
Modified By -
Modified By -
.........

0 Votes 0 ·

Thnx everyone for your help. Cheers.

0 Votes 0 ·
RaytheonXie-MSFT avatar image
0 Votes"
RaytheonXie-MSFT answered TanPhatHuynh-4230 edited

Hi @TanPhatHuynh-4230 ,
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.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks. How do I view all fields in $listItem?

Eg..

$listItem[Author]
$listItem["ID"]
$listItem["Title"]
....



0 Votes 0 ·

Hi @TanPhatHuynh-4230 ,
You need to add the column name to the foreach loop,such as

 foreach($listItem in $listItems)  
 {  
     Write-Host "Author - " $listItem["Author"] "ID - " $listItem["ID"] "Title - " $listItem["Title"] 
 } 



0 Votes 0 ·

Hi @TanPhatHuynh-4230 ,
I am checking to see how things are going there on this issue.

0 Votes 0 ·
Show more comments