SharePoint Online迭代文档库CSOM

时间:2015-01-29 15:18:53

标签: powershell listitem sharepoint-online csom sharepointdocumentlibrary

我正在尝试迭代文档库并设置每个文档/项目以继承权限(目前每个doc / item都使用唯一权限)。

我能够获得特定的文档库,但是我不能迭代其中的每个项目/文档。这就是我到目前为止所做的:

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

对我应采取的方法提出任何建议都将不胜感激。

1 个答案:

答案 0 :(得分: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()方法有关,需要传递给它的附加参数。我认为只有在处理lists / listItems时才需要这样做。没有花太多时间调查确切的根本原因,但我已经重新设计了我的解决方案并使其变得更简单:

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()
    }

这就像现在的魅力一样。