I have a need to update the TimeLastModified on many files in our sharepoint sites, but I'm struggling to make it work.
Background: After migrating to Sharepoint Online we experienced performance issues due to permissions complexity. Our migration provider fixed that by moving everything to new sites with simplified permission structures. Unfortunately in that move all the files picked up the TimeLastModified as the time they got copied to the new site. Our users are not happy with this.
So, I created a powershell script to pull all the original timestamps from the old site along with filenames and paths and stored that in a spreadsheet.
In another script I am now iterating through all the files in a new site, if a file hasn't been modified since the move I find its matching entry in the spreadsheet, retrieve the original last modified datetime and then update the files TimeLastModified to the correct original datetime.
Problem: It is that last piece I can't make work, as in the actual update of the TimeLastModified.
The approach I took (skipping over loops and unimportant stuff below) was:
#Get Sub-folders from top-level down
Get-PnPProperty -ClientObject $Folder -Property ServerRelativeUrl, Folders | Out-Null
#loop through all the folders getting files
$Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeUrl -ItemType File
$Files | ForEach-Object { #.....
# load object, decide if need update, get $TimeLastMod from spreadsheet.....
$fileFolderItem = Get-PnPFolderItem -FolderSiteRelativeUrl $filePath -ItemName $fileName -ItemType File
$fileFolderItem.TimeLastModified = $OldDateTime
$fileFolderItem.Update()
However that gives error:
'TimeLastModified' is a ReadOnly property.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Further searching indicated I should be able to update the value if it was a ListItem I was trying to update using something like Set-PnPListItem -List $ListName -Identity $fileItem -Values @{ TimeLastModified = $OldDateTime } -SystemUpdate
, so how do I change my original approach to get a List object I can use? I've had a number of attempts at taking the $fileFolderItem and trying to use Get-PnPListItem
but I'm really not understanding what parameters to put in their based on what I have retrieved via Get-PnPFolderItem
. Maybe that is where it is wrong and at the very top level I should be retrieving the folders and files as a different type of object using some list based thing?
Can anyone please give me any pointers on how to make this work?
(I have been a programmer for 25 years but have not worked with asp.net at all, and only a little bit dabbling with powershell so I'm not getting what this Lists versus the other objects stuff is all about)
Thanks, Bryce.
[Edit - 29/01/2021: update/progress] working with G42's tip in the comments.
Turns out I already had a list at highest level which was used to retrieve all the folder paths (ServerRelativeUrl) into an array.
Further working with that array to try to get the files without hitting "The attempted operation is prohibited because it exceeds the list view threshold." (the limit is 5000 in sharepoint online for us - we have around 220,000 folders however). So essentially as below I have Get-PnPList
followed by Get-PnPListItem
:
$Listname = "Documents"
$Library = Get-PnPList -Identity $ListName -Includes RootFolder
#<loaded array and now try to get files from one folder>
$RelativeURL = myArray[1]
Get-PnPListItem -List $Library -FolderServerRelativeUrl $RelativeURL | Foreach-Object {#do processing..... etc...
# value of $RelativeURL is "/sites/ZData/Shared Documents/Health&Safety"
# - also tried with $RelativeURL ending in '/' or '/*', still no good.
So using the -FolderServerRelativeUrl
to try to limit the result set to just one folder doesn't do what I hoped, I exceed the list view threshold. Maybe I'm not using it right.
Do I need to somehow create a small list for of just one folders objects and use that as input to Get-PnPListItem? If so how do I go about that as I've failed in all my attempts so far?
Or is some other solution required to tackle this?
Cheers, Bryce.
question from:
https://stackoverflow.com/questions/65893880/update-sharepoint-files-timelastmodified-cannot-convert-between-errors