How to get items from a Sharepoint Online list using PowerShell?

前端 未结 1 710
清歌不尽
清歌不尽 2021-01-01 07:07

I\'m writing a PowerShell script that connects to a SharePoint website and I\'m trying to get the items on a list. Here\'s my code:

$Context = New-Object Mic         


        
相关标签:
1条回答
  • 2021-01-01 07:25

    The following example demonstrates how to retrieve list items using the SharePoint CSOM API in PowerShell:

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
    
    
    Function Get-SPOContext([string]$Url,[string]$UserName,[string]$Password)
    {
        $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
        $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
        $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
        return $context
    }
    
    Function Get-ListItems([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {
        $list = $Context.Web.Lists.GetByTitle($listTitle)
        $qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
        $items = $list.GetItems($qry)
        $Context.Load($items)
        $Context.ExecuteQuery()
        return $items 
    }
    
    
    
    $UserName = "jdoe@contoso.onmicrosoft.com"
    $Password = Read-Host -Prompt "Enter the password"    
    $Url = "https://contoso.sharepoint.com/"
    
    
    $context = Get-SPOContext -Url $Url -UserName $UserName -Password $Password
    $items = Get-ListItems -Context $context -ListTitle "Tasks" 
    foreach($item in $items)
    {
       #...
    }
    $context.Dispose()
    
    0 讨论(0)
提交回复
热议问题