1

I am trying to iterate though a document library and set each document/items within to inherit permissions (at the moment each doc/item is using uniquer permissions).

I am able to get the specific document library that I am interesting in, however I cannot at the moment iterate though each of the items/documents within it, but here is what I have so far:

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)


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


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 
            }


    }
}

It is the foreach loop I am having trouble at the moment as I am not sure how to loop though each of the items/documents in the document library.

Any suggestions on the approach I should take would be much appreciated.

1

You should load the items before using it.

 $items = $list.Items
 $ctx.Load($items)
 $ctx.ExecuteQuery()
 foreach ($item2 in $items)
 {
     #list the items/documents  in the document library
     write-host $item2.Title 
 }

For Inheriting Permission you can use ResetRoleInheritance

$item2.ResetRoleInheritance()
  • 1
    It should be ResetRoleInheritance instead of BreakInheritance – Nadeem Yousuf Jan 30 '15 at 11:17
  • not a problem, I updated your answer. – Nadeem Yousuf Jan 30 '15 at 11:20
  • the script fails at "ctx.Load($item)" with error code: Cannot find an overload for "Load" and the argument count: "1". At C:\Users\setup\Desktop\Test-BreakInheritence-Script-SPOnline\Permissions-Inh eritence.ps1:56 char:3 + $ctx.Load($items) + ~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest – Dev P Jan 30 '15 at 13:15
  • Yep I have corrected the typo, however the it still errors on the line "$ctx.Load($items)": Cannot find an overload for "Load" and the argument count: "1". At C:\Users\setup\Desktop\Test-BreakInheritence-Script-SPOnline\Permissions-Inh eritence.ps1:38 char:3 + $ctx.Load($items) + ~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest – Dev P Jan 30 '15 at 14:16
1

The error I was getting:

Cannot find an overload for "Load" and the argument count: "1". At C:\Users\setup\Desktop\Test-BreakInheritence-Script-SPOnline\Permissions-Inh eritence.ps1:38 char:3 + $ctx.Load($items) + ~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId :

Was to do with the Load() method requiring additinal parameters passed into it. I think this is only required when dealing with lists/listItems. Did not spend too much time investigating the exact root cause, but I have reworked my solution and made it simpler:

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

This works like a charm now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.