Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $url = 'https://domain.sharepoint.com/'
- $ListTitle = 'List Name'
- $folderName = 'Folder Name'
- # Paths to SDK. Please verify location on your computer.
- Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll"
- Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
- # Login
- $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
- $username = "myuser@domain.com"
- $AdminPassword = "my password"
- $password = ConvertTo-SecureString -string $AdminPassword -AsPlainText -Force
- $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
- # Fetch list
- $ll=$ctx.Web.Lists.GetByTitle($ListTitle)
- $ctx.Load($ll)
- $ctx.ExecuteQuery()
- # Find folder
- $folder = $ctx.Web.GetFolderByServerRelativeUrl($ctx.Web.ServerRelativeUrl + $listTitle + '/' + $folderName + '/')
- $ctx.Load($folder)
- $ctx.ExecuteQuery();
- # Fetch items
- $spQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
- ## View XML
- $qCommand = @"
- <View Scope="RecursiveAll">
- <Query>
- <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
- </Query>
- <RowLimit Paged="TRUE">5000</RowLimit>
- </View>
- "@
- ## Page Position
- $position = $null
- ## All Items
- $allItems = @()
- Do
- {
- $spQuery.ListItemCollectionPosition = $position
- $spQuery.ViewXml = $qCommand
- ## Executing the query
- $spQuery.FolderServerRelativeUrl = $folder.ServerRelativeUrl
- $itemki = $ll.GetItems($spQuery)
- $ctx.Load($itemki)
- $ctx.ExecuteQuery()
- # Loop through all items
- $noOfObject = $itemki.Count
- for($j=0;$j -lt $itemki.Count ;$j++)
- {
- $itemki[$j].ResetRoleInheritance()
- if($j % 500 -eq 0) # Processing 500 items at a time.
- {
- $ctx.ExecuteQuery()
- }
- }
- $ctx.ExecuteQuery()
- ## Getting the position of the previous page
- $position = $itemki.ListItemCollectionPosition
- Write-Host $position.PagingInfo $position.TypeId
- # Adding current collection to the allItems collection
- $allItems += $itemki
- }
- # the position of the last page will be Null
- Until($position -eq $null)
Add Comment
Please, Sign In to add comment