通过标题获取SharePoint列表

时间:2018-09-10 15:31:34

标签: powershell sharepoint-online

尝试编写一个函数,该函数按其标题获取SharePoint列表并返回该列表。我收到错误消息,我不确定为什么,因为我知道列表存在,并且在遍历所有列表和显示标题时可以看到它。这是我的代码:

function Get-List {
    param (
        [parameter(Mandatory=$true, ValueFromPipelineByPropertyName = $true)][string]$listTitle
    )

    process {
        $lists = $Context.Web.Lists
        $Context.Load($lists)
        $Context.ExecuteQuery()

        $list = $lists | Where {$_.Title -eq $listTitle }
        $Context.Load($list)
        $Context.ExecuteQuery()

        return $list
    }

    end {}
}

$User = "xyz@xyz.onmicrosoft.com"
$SiteURL = "https://xyz.sharepoint.com/sites/xyz/"

Add-Type -Path "C:\Program Files\WindowsPowerShell\Modules\SharePointPnPPowerShellOnline\2.26.1805.1\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\WindowsPowerShell\Modules\SharePointPnPPowerShellOnline\2.26.1805.1\Microsoft.SharePoint.Client.Runtime.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

Get-List "List Title"

这是错误:

  

format-default:集合尚未初始化。它没有   已请求或尚未执行请求。可能需要   明确要求。

0 个答案:

没有答案