0

So obviously I am lacking all skills to build or even search for the solution to my issue. I hope there's someone here who can help me.

I need to remove the retention label on a couple of files in a SharePoint folder. The files are nested inside subfolders. I have found a script that loops through the folders and resets the label. However, the 5.000 items list view threshold of SharePoint Online prevents me from running the script smoothly.

So I came up with a solution.

First I am caching all list items into an array.

$ListItems = Get-PnPListItem -List Documents -PageSize 500

This is how an item inside this array looks like: enter image description here

So now I have this.

Next, I am looping through all folders I receive only the GUID from a file (from SharePoint). Not the ID. So while I am looping through the I want to lookup the item in the array by using the GUID. I thought of using a filter action inside the foreach. Eventually, I need to have the ID value (number) of the file so I can perform a Set-PnPListItem action.

I am getting stuck at the filter action inside the foreach. I simply can't get it to work after struggling and searching for 2.5 hours.

This is what I have now:

       $itemsColl=Get-PnPFolderItem -FolderSiteRelativeUrl $newFolderURL -ItemType File

       foreach($ArrayItem in $itemsColl){
       $GUID = $ArrayItem.UniqueId.Guid
       $resultaat = $ListItems | Where {$_["FieldValues"]["GUID"] -eq $ArrayItem.UniqueId.Guid} # DOESN'T WORK
       $resultaat = $ListItems | Where-Object $a = @($_.FieldValues.GUID) {$ListItems | Where-Object $a -eq $ArrayItem.FieldValues.Guid} # DOESN'T WORK
       Write-Host $resultaat.FieldValues.ID
       }

Unfortunately I can't get it to work. I doubt if it is because of the nested FieldValues, or that my nested $_ references won't work. I hope someone here can help me fix it!

Thanks in advance, Joost

1 Answer 1

0

OK I managed to find a solution. Credits link included at the bottom of the script.

Please note: since you can remove the retention label from the top-level folder from within the SharePoint Online website, which will be propagated to all child folders (subfolders), I use this script only to remove retention labels from files. If you prefer this script to also remove the retention label from folders, simply remove the if-else condition.

#Set Parameters
$SiteURL = "https://yourtenant.sharepoint.com/sites/yoursitename"
$FolderServerRelativeURL = "/sites/yoursitename/Shared Documents/yourfoldername"
   
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL
 
#Get the Folder to download
$Folder = Get-PnPFolder -Url $FolderServerRelativeURL -Includes ListItemAllFields.ParentList
#Get the Parent List
$List = $Folder.ListItemAllFields.ParentList
 
#Get all Items from the folder - with progress bar
$global:counter = 0;
$ListItemsNew = Get-PnPListItem -List $List -PageSize 500 -Fields FileLeafRef -ScriptBlock { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete `
                ($global:Counter / ($List.ItemCount) * 100) -Activity "Getting Items from List:" -Status "Processing Items $global:Counter to $($List.ItemCount)";} | Where {$_.FieldValues.FileRef -like "$($FolderServerRelativeUrl)*"} 
Write-Progress -Activity "Completed Retrieving Items from Folder $FolderServerRelativeURL" -Completed
 
#Get All Files and Folders from the Folder Recursively
$ListItemsNew | ForEach-Object {
    if($_.FileSystemObjectType -eq 'Folder'){
    # Do nothing
    Write-Host -ForegroundColor Red "Skipping folder"
    }else{
    Write-host -Foregroundcolor Green  "Processing: " $_.FieldValues.FileRef
    Write-host -Foregroundcolor Green  "ID: " $_.FieldValues.ID
    Set-PnPListItem -List Documents -Identity $_.FieldValues.ID -ClearLabel | Out-Null
    }
}

#Read more: https://www.sharepointdiary.com/2017/02/sharepoint-online-get-list-items-from-folder-using-powershell.html#ixzz7PFXVH4rj

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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