How to get items from a sharepoint online list usi

2019-02-15 11:49发布

I'm writing a little 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 Microsoft.SharePoint.Client.ClientContext($SiteURL)

$SitePassword = ConvertTo-SecureString $SitePwd -AsPlainText -Force
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($SiteUser,$SitePassword)
$Context.Credentials = $Creds

$web = $Context.Web 
$Context.Load($web)
$Context.ExecuteQuery()

$List = $Context.Web.Lists.GetByTitle('Déposes')
$Context.Load($List)
$Context.ExecuteQuery()

But now i'm stuck with this $List Object and I can't find a way through, for now, I just want to display my list's items.

Thanks!

1条回答
甜甜的少女心
2楼-- · 2019-02-15 12:00

The following example demonstrates how to retrieve list items using 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()
查看更多
登录 后发表回答