【问题标题】:SharePoint Online Iterating through Document Libraries CSOMSharePoint Online 迭代文档库 CSOM
【发布时间】:2015-03-28 21:09:43
【问题描述】:

我正在尝试遍历文档库并将每个文档/项目设置为继承权限(目前每个文档/项目都使用唯一权限)。

我能够获得特定的文档库,但是我无法遍历其中的每个项目/文档。这是我目前所拥有的:

Add-Type -Path "Libraries\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "Libraries\Microsoft.SharePoint.Client.Runtime.dll" 
Add-Type -Path "Libraries\Microsoft.SharePoint.Linq.dll"
Add-Type -Path "Libraries\Microsoft.SharePoint.dll"

$webUrl = "https://test.sharepoint.com/sites/testsite" 
$username = "####"
$password = "####"
$securePass = ConvertTo-SecureString $password -AsPlainText -Force
$listname = "TestDoc";

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePass)

#variables
$web = $ctx.Web
$lists = $web.Lists
$ctx.Load($lists)
$ctx.Load($web)
$ctx.ExecuteQuery()

#print web URL
write-host `n "Web Url:" `n $web.Url

foreach ($list in $lists)
{

    if ($list.Title -eq "TestDoc")
    {
        #print list name if found
        write-host `n "Found the list:" `n $list.Title `n


            #attempting to iterate through items in the document library 
            foreach ($item2 in $list.Items)
            {
                #list the items/documents  in the document library
                write-host $item2.Title 
            }


    }
}

这是我目前遇到问题的 foreach 循环,因为我不确定如何遍历文档库中的每个项目/文档。

任何关于我应该采取的方法的建议将不胜感激。

【问题讨论】:

    标签: powershell listitem sharepoint-online csom sharepointdocumentlibrary


    【解决方案1】:

    我得到的错误:

    找不到“Load”的重载和参数计数:“1”。在 C:\Users\setup\Desktop\Test-BreakInheritence-Script-SPOnline\Permissions-Inh eritence.ps1:38 char:3 + $ctx.Load($items) + ~~~~~~~~~~~ ~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId :

    与需要传入附加参数的 Load() 方法有关。我认为这仅在处理列表/列表项时才需要。没有花太多时间调查确切的根本原因,但我重新设计了我的解决方案并使其更简单:

    Add-Type -Path "Libraries\Microsoft.SharePoint.Client.dll" 
    Add-Type -Path "Libraries\Microsoft.SharePoint.Client.Runtime.dll" 
    Add-Type -Path "Libraries\Microsoft.SharePoint.Linq.dll"
    Add-Type -Path "Libraries\Microsoft.SharePoint.dll"
    
        $webUrl = "https://test.sharepoint.com/sites/testsite" 
        $username = "####"
        $password = "####"
        $securePass = ConvertTo-SecureString $password -AsPlainText -Force
        $listname = "TestDoc"
    
        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
        $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePass)
    
    
        #get the List/DocLib and load it for use
        $listUpdate = $ctx.Web.Lists.GetByTitle($listname)
        $ctx.Load($listUpdate)
        $ctx.ExecuteQuery()
    
        #CAML Query to get all items inclusing sub-folders
        $spQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
        $spQuery.ViewXml = "<View Scope='RecursiveAll' />";
        $itemki = $listUpdate.GetItems($spQuery)
        $ctx.Load($itemki)
        $ctx.ExecuteQuery()
    
        #iterating through the items and reseting permission inheritence
        for($j=0; $j -lt $itemki.Count; $j++)
        {
            $itemki[$j].ResetRoleInheritance()
            $ctx.ExecuteQuery()
        }
    

    现在这就像一个魅力。

    【讨论】:

    • 谢谢。这帮助我构建了一个脚本,用于修改 SPO 文档库中文档的标题。
    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 2020-04-28
    • 1970-01-01
    • 2017-12-01
    • 2017-01-30
    • 1970-01-01
    • 2014-08-04
    • 2017-04-04
    相关资源
    最近更新 更多