SharePoint Online: Khắc phục "Đã xảy ra lỗi khi liệt kê qua bộ sưu tập: Bộ sưu tập chưa được khởi tạo. Nó chưa được yêu cầu hoặc yêu cầu chưa được thực thi. Có thể cần phải được yêu cầu rõ ràng" trong PowerShell
Sự cố: Lỗi trong tập lệnh CSOM PowerShell của SharePoint Online:
“Đã xảy ra lỗi khi liệt kê thông qua một bộ sưu tập: Bộ sưu tập chưa được khởi tạo. Nó chưa được yêu cầu hoặc yêu cầu chưa được thực hiện. Có thể cần phải yêu cầu rõ ràng”
Nguyên nhân cốt lõi: Lỗi “Đã xảy ra lỗi khi liệt kê thông qua một bộ sưu tập: Bộ sưu tập chưa được khởi tạo. Nó chưa được yêu cầu hoặc yêu cầu chưa được thực hiện. Nó có thể cần được yêu cầu rõ ràng” thường xảy ra khi bạn đang cố truy cập một đối tượng trong bộ sưu tập trước khi nó được khởi tạo hoặc tải. Mọi bộ sưu tập phải được tải rõ ràng trước khi truy cập các thành viên của nó.
Tập lệnh có vấn đề:
Tập lệnh PowerShell này cố gắng lấy các cột thuộc loại nội dung.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\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
}
Ở đây, trong tập lệnh trên, chúng tôi đang cố gắng lấy các cột từ một loại nội dung mà không tải bộ sưu tập trường một cách rõ ràng, vì vậy nó dẫn đến lỗi!
Bộ sưu tập không có được khởi tạo. Nó chưa được yêu cầu hoặc yêu cầu chưa được thực hiện. Bạn có thể cần phải yêu cầu điều này một cách rõ ràng.
Tại dòng:24 char:13
+ ForEach($Field in $ContentType.Fields)
+ ~~~~~~
+ CategoryInfo : OperationStopped: (:) [], CollectionNotInitializedException
+ CompleteQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException
Giải pháp:
Để giải quyết lỗi, bạn cần đảm bảo rằng bộ sưu tập đã được khởi tạo và tải đúng cách trước khi bạn cố gắng truy cập nó. Điều này thường có thể được thực hiện bằng cách gọi phương thức thích hợp để tải bộ sưu tập trước khi gọi phương thức Thực thi trong SharePoint Online.
Đây là Tập lệnh đã sửa, trong đó chúng tôi đã tải bộ sưu tập $ContentType.Fields và giải quyết vấn đề.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\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
}
Tương tự, trong PnP PowerShell, chúng ta phải sử dụng lệnh ghép ngắn Get-PnPProperty để tải một bộ sưu tập.
#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
Bằng cách khởi tạo và tải bộ sưu tập đúng cách trước khi truy cập, bạn có thể tránh được lỗi “Đã xảy ra lỗi khi liệt kê thông qua bộ sưu tập” và truy xuất thành công các thuộc tính được liên kết với một đối tượng trong SharePoint Online bằng PowerShell.
