1

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

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

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 循环,因为我不确定如何遍历文档库中的每个项目/文档。

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

4

1 回答 1

0

我得到的错误:

找不到“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()
    }

这现在就像一个魅力。

于 2015-02-02T14:10:09.507 回答