Uploading file to Sharepoint 2013 (on prem) – results in weird issue

Hey all,

I hope this isn’t outside the scope of this sub, if it is then if someone could point me to the right sub (maybe a sharepoint sub or web dev sub?) I would be appreciative.

Here is the code I am using to upload files to SharePoint. This seems to be the only way to utilize the Microsoft.SharePoint.PowerShell PS Snap-in:

 try { Write-Host -ForegroundColor Cyan "[ ] Verifying SharePoint site: $siteUrl/$fileSymbol" $sharePointSite = Get-SPWeb "$siteUrl/$fileSymbol" } catch { Write-Host -ForegroundColor Red "[-] ERROR: Could not find SharePoint Site: $siteUrl/$fileSymbol" continue } if ($null -ne $sharePointSite) { Write-Host -ForegroundColor Green "[+] SUCCESS: Found SharePoint site: $siteUrl/$fileSymbol" try { $spList = $sharePointSite.Lists["Special Documents"] $fileStream = ([System.IO.FileInfo](Get-Item $file.FullName)).OpenRead() $contents = New-Object byte[] $fileStream.Length $fileStream.Read($contents, 0, [int]$fileStream.Length); $fileStream.Close(); $spFolder = $sharePointSite.getfolder($spList.Title) ##Changed code here to get subfolder $spFile = $spFolder.Files.Add($file.Name, $contents, $true) $spItem = $spFile.Item $spFile.Item.File.CheckOut() $spItem["ContentTypeId"] = "0x010100F6A6EC4A443648B393FC283765A93B38005EF600481C3047CEBFAB95778EF6340C" $spItem.Update() $spFile.Item.File.CheckIn("Set content type for upload", [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn) Write-Host -ForegroundColor Green "[+] SUCCESS: Uploaded $($file.Name) to Sharepoint site: $sharePointSite" } catch { $ErrorMessage = $_.Exception.Message Write-Host -ForegroundColor Red "[-] ERROR: Upload of $($file.Name) to SharePoint site: $sharePointSite FAILED." Write-Host -ForegroundColor Red "Exception:" Write-Host -ForegroundColor Red $ErrorMessage } } 

The above code “works”. The file shows up in the SharePoint document library where it is supposed to go. However, when I click on the file I get the following:

<div class="ms-vb">Unable to render the data. If the problem persists, contact your web server administrator.</div><br/><br/>Correlation ID:7137dd9e-8d6e-60de-17aa-9eaddf6acc98<br/> 

Looking online, this seems to be related to a cumulative update or configuration issue in SharePoint, however I have been re-engineering this from spaghetti-code that worked oh-so-long-ago and now doesn’t.

Is there something specific with the snap-in that I am missing? It seems that maybe there is/was something being set on the file when it gets uploaded that avoids this issue. Any ideas?

Edit:

I’m seeing some differences in the spaghetti-code vs my code.

Here is the function that moves the file within the spaghetti-code:

function MoveFile($file, $destinationLibrary, $destinationName) { if ($commit -eq $true) { $newFile = $destinationLibrary.RootFolder.Files.Add($file.Name, $file.OpenBinary(), $file.Properties, $true) if ($newFile.Item -ne $null) { $file.Delete() } } $destinationMessage = $destinationLibrary.Title if (![string]::IsNullOrEmpty($destinationName)) { $destinationMessage = $destinationName } Log "Moved $($file.Name) to $destinationMessage" } 

My code:

 $fileStream = ([System.IO.FileInfo](Get-Item $file.FullName)).OpenRead() $contents = New-Object byte[] $fileStream.Length $fileStream.Read($contents, 0, [int]$fileStream.Length); $fileStream.Close(); $spFolder = $sharePointSite.getfolder($spList.Title) ##Changed code here to get subfolder $spFile = $spFolder.Files.Add($file.Name, $contents, $true) $spItem = $spFile.Item $spFile.Item.File.CheckOut() $spItem["ContentTypeId"] = "0x010100F6A6EC4A443648B393FC283765A93B38005EF600481C3047CEBFAB95778EF6340C" $spItem.Update() $spFile.Item.File.CheckIn("Set content type for upload", [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn) 

But when I use a test file with gci “file”, I don’t get any values when doing $file.Properties

submitted by /u/Marquis77
[link] [comments]

Leave a Reply