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

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

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

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


问题: SharePoint Online CSOM PowerShell 脚本中出现错误:
“枚举集合时出错:集合尚未初始化。尚未请求或请求尚未执行。可能需要明确请求”

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

根本原因:错误“枚举集合时发生错误:集合尚未初始化。尚未请求或请求尚未执行。 “可能需要显式请求”通常发生在您尝试在初始化或加载集合中的对象之前访问该对象时。每个集合在访问其成员之前都必须显式加载。

有问题的脚本:
此 PowerShell 脚本尝试获取内容类型的列。


#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/sites/marketing"
$ContentTypeID="0x01002A7A908ACAB0054880702EE263AC762B"
 
#Get 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 content type
$ContentType = $Ctx.web.ContentTypes.GetById($ContentTypeID)
$Ctx.Load($ContentType)
$Ctx.ExecuteQuery()
 
If($ContentType -ne $Null)
{
    #Get columns from the content type
    ForEach($Field in $ContentType.Fields)
    {           
        Write-Host -f Green $Field.Title
    } 
}
else
{
    Write-host "Content Type '$ContentTypeName' doesn't exist!'" -f Yellow
}

在上面的脚本中,我们尝试从内容类型获取列,而不显式加载字段集合,因此最终会出现错误!
该集合没有已初始化。尚未请求或请求尚未执行。可能需要明确请求。
在第 24 行 char:13
+ ForEach($ContentType.Fields 中的 $Field)
+ ~~~~~~
+ CategoryInfo : 操作已停止: (:) [],CollectionNotInitializedException
+ ExcellentQualifiedErrorId:Microsoft.SharePoint.Client.CollectionNotInitializedException

解决方案:

要解决该错误,您需要确保在尝试访问集合之前已正确初始化并加载该集合。这通常可以通过在调用 SharePoint Online 中的 ExecuteQuery 方法之前调用适当的方法来加载集合来完成。

这是更正后的脚本,我们在其中加载了 $ContentType.Fields 集合并解决了问题。


#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/sites/marketing"
$ContentTypeID="0x01002A7A908ACAB0054880702EE263AC762B"
 
#Get 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 content type
$ContentType = $Ctx.web.ContentTypes.GetById($ContentTypeID)
$Ctx.Load($ContentType)
$Ctx.ExecuteQuery()
 
If($ContentType -ne $Null)
{
    #Get Columns from the content type
    $Ctx.Load($ContentType.Fields)
    $Ctx.ExecuteQuery()

    #Get columns from the content type
    ForEach($Field in $ContentType.Fields)
    {           
        Write-Host -f Green $Field.Title
    } 
}
else
{
    Write-host "Content Type '$ContentTypeName' doesn't exist!'" -f Yellow
}

同样,在 PnP PowerShell 中,我们必须使用 Get-PnPProperty cmdlet 来加载集合。


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$ContentTypeName ="Item"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get the List content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName
 
#Load the "Fields" collection to retrieve All Fields from the Content Type
$ContentTypeFields = Get-PnPProperty -ClientObject $ContentType -Property Fields

#Get Field Title, Internal Name and ID
$ContentTypeFields | Select Title, InternalName, ID

通过在访问集合之前正确初始化和加载集合,您可以避免“枚举集合时发生错误”错误,并使用 PowerShell 成功检索与 SharePoint Online 中的对象关联的属性。

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

取消回复欢迎 发表评论:

关灯