$FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
if(!($file -match $pat)){
$FileCreationInfo.URL = $newfile
}else{
$FileCreationInfo.URL = $File
}
$Upload = $DestinationFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
Write-host "Adding the meta data to the document in O365"
$ListItem = $Upload.ListItemAllFields;
$Listitem["Modified"] = $ModDateSP
$Listitem["Created"] = $DateSP
$Listitem["Author"] = $author
$Listitem["Editor"] = $lastSaved
$ListItem.Update()
$Context.Load($Upload)
$Context.ExecuteQuery()
if($Upload.CheckOutType -ne "none")
{
$Upload.CheckIn("Checked in by Administrator", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
}
$output=([System.Uri]$SiteURL).PathAndQuery
$folderurl=$output+'/'+$DocLibName+$folderrelativepath
$sourcefolder = $Context.web.GetFolderByServerRelativeUrl($folderurl)
$Context.Load($sourcefolder)
$Context.ExecuteQuery()
$Context.Load($sourcefolder.Files)
$Context.ExecuteQuery()
foreach($item in $sourcefolder.Files){
if($item.name -eq $file.Name){
Write-host "File Found"
$context.Load($item)
$context.ExecuteQuery()
$Versions = $item.Versions
$context.Load($Versions)
$context.ExecuteQuery()
write-host $Versions.count
}
}
I have this code above which uploads a file changes the metadata checks it in and then counts the versions of the file.
The problem is when I count the versions it does not contain any saying the version count is 0 but when I go into the document library and check the versions manually there are 2.
This is annoying because I am trying to delete the first version and as it is not finding any versions I cannot do this.
Any ideas?


