如何使用 PowerShell 从 Sharepoint Online 列表中获取项目? [英] How to get items from a Sharepoint Online list using PowerShell?
本文介绍了如何使用 PowerShell 从 Sharepoint Online 列表中获取项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个连接到 SharePoint 网站的 PowerShell 脚本,我正在尝试获取列表中的项目.这是我的代码:
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 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()
但现在我被这个 $List
对象困住了,我找不到办法通过.目前,我只想显示我的列表项.
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.
推荐答案
以下示例演示了如何在 PowerShell 中使用 SharePoint CSOM API 检索列表项:
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()
这篇关于如何使用 PowerShell 从 Sharepoint Online 列表中获取项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文