gpt4 book ai didi

powershell - SharePoint CSOM PowerShell不返回Web.Folders属性

转载 作者:行者123 更新时间:2023-12-02 23:12:00 30 4
gpt4 key购买 nike

此处的目标是从此PowerShell代码开始访问Web.Folders属性。

Connect-SPOnline –Url https://mysite.sharepoint.com;
$web = Get-SPOWeb;
$web | Get-Member | sort name

从该列表成员中,我可以看到具有以下内容的“文件夹”属性
Definition:
----------
Microsoft.SharePoint.Client.FolderCollection,
Microsoft.SharePoint.Client,
Version=16.1.0.0,
Culture=neutral,
PublicKeyToken=71e9bce111e9429c Folders {get;}

因此,我是否应该能够使用以下代码片段检索“文件夹属性”?
$ctx = Get-SPOContext;
$folders = $web.Folders;
$ctx.Load($folders);
$ctx.ExecuteQuery();
$folders;
$folders仅返回以下未初始化的消息。
format-default : The collection has not been initialized. It has not been requested or the request has not been
executed. It may need to be explicitly requested.
+ CategoryInfo : NotSpecified: (:) [format-default], CollectionNotInitializedException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Comma
nds.FormatDefaultCommand

最佳答案

不要让此错误消息使您感到困惑。即使未从服务器请求客户端对象(文件夹集合)时,通常会发生此错误,但在以下特定情况下,由于以下原因会发生此错误:

$folders

因此,使用指定的代码片段文件夹集合可以从服务器正确检索:
$web = $ctx.Web
$folders = $web.Folders
$ctx.Load($folders)
$ctx.ExecuteQuery()

为了确定是否已从服务器请求客户端对象,请使用 ClientObject.IsObjectPropertyInstantiated method ,例如:
if ($web.IsObjectPropertyInstantiated('Folders') -eq $true)
{
Write-Host "Folder collection has been loaded"
#...

}

以下示例演示如何访问collection中的文件夹及其属性。

示例1.如何迭代文件夹集合并打印其名称:
$folders.GetEnumerator() | % { 
Write-Host "Folder name: $($_.Name)"
}

示例2.如何通过索引访问文件夹并打印其名称:
if($folders.Count -gt 0){
$folder = $folders[0] #get first item
Write-Host "Folder name: $($folder.Name)"
}

关于powershell - SharePoint CSOM PowerShell不返回Web.Folders属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36585936/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com