当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:修复 PowerShell 中的“集合尚未初始化。尚未请求或请求尚未执行。可能需要显式请求”错误

作者:精品下载站 日期:2024-12-14 15:03:00 浏览:15 分类:玩电脑

SharePoint Online:修复 PowerShell 中的“集合尚未初始化。尚未请求或请求尚未执行。可能需要显式请求”错误


错误:集合尚未初始化。尚未请求或请求尚未执行。可能需要明确要求。

[玩转系统] SharePoint Online:修复 PowerShell 中的“集合尚未初始化。尚未请求或请求尚未执行。可能需要显式请求”错误

根本原因:

您必须在尝试检索对象的属性之前加载该对象!

解决方案:

“集合尚未初始化。尚未请求或请求尚未执行。当您尝试在加载和执行 SharePoint Online 中的对象之前访问该对象时,可能需要显式请求”错误。当您在向服务器发出请求之前尝试访问对象集合(例如项目列表)时,可能会发生这种情况。要修复此错误,您需要显式请求集合,然后执行查询来加载数据。

以下是解决该问题的方法:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Parameters
$SiteURL = "https://Crescent.sharepoint.com"
$UserName = "[email protected]"
$Password = "Password goes here"
$SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
 
#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
 
#Get All Fields from the List
$List = $Ctx.Web.Lists.GetByTitle("Documents")

#Load List Fields collection
$Ctx.Load($List.Fields)
$Ctx.ExecuteQuery()

ForEach($Field in $List.Fields)
{
    Write-host $Field.Title
}

请注意,如果您跳过第 19 行和第 20 行,您最终会收到错误集合尚未初始化!我们再举一个例子,从 SharePoint Online 网站检索所有列表名称:


Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Projects"
 
#Setup Credentials to connect
$Cred = Get-Credential

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
#Get all List names from the site
$Lists = $Ctx.web.Lists
ForEach($List in $Lists)
{
    Write-host $List.Title
}

上面的脚本失败并显示错误消息:

在这种情况下,我们应该首先加载 Lists 集合。因此,要解决该问题,请使用以下命令:


Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Projects"
 
#Setup Credentials to connect
$Cred = Get-Credential

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
#Get all List names from the site
$Lists = $Ctx.web.Lists
$Ctx.Load($Lists)
$Ctx.ExecuteQuery()

ForEach($List in $Lists)
{
    Write-host $List.Title
}

因此,请确保在循环内访问集合对象之前已加载并执行它们!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯