0

I want to upload files via PowerShell script that have been downloaded and edited, primarily I want to maintain the original metadata. Does anyone know what method this upload button uses from 2010? It is the closest to what I need to do. I am still trying to figure out other methods for upload that recognize the metadata but really wish I could put this exact method into my script and modify it from there to achieve what I really need. enter image description here

Edit:

So I found I need to use a HashTable to give the SpFileCollection.Add method the properties. I need to now understand how to turn the hidden XML metadata in the file download into a hash table. I wish that I could just see how they did it in the ribbon

  • Are you talking about metadata already in SharePoint? If you are uploading a document into a library in SharePoint, and the document was already there, and you have not changed the name, from my understanding, SharePoint will just overwrite the document that's already in the library, and the existing metadata should not be affected. – Dylan Cristy Aug 1 '16 at 13:54
  • Not quite, when I do that with PowerShell it does not work properly. When I use the ribbon it works fine in IE. I want to do this in a PowerShell script to do over 100 file uploads at once as part of a very large scripted operation – Michael Ellis Aug 1 '16 at 13:57
  • What happens when you upload through Powershell? – Dylan Cristy Aug 1 '16 at 13:58
  • It works, I just edited my post. But the metadata is hidden XML at the bottom of the doc when I open it in notepad after download/upload/redownload. I want this metadata to magically connect like the ribbon. Can I expose how it does that? – Michael Ellis Aug 1 '16 at 14:04
0

The following code shows how I managed to get at all except Enterprice Keywords uploading via script. Now I must fine tune it. I hope this helps someone else one day!

$webClient.DownloadFile($file.docURL, $file.localPath)
$spWeb = Get-SPWeb -Identity $file.domain
$List = $spWeb.Lists[$docLibName]
$listFile = $List.get_Items() | ?{$_.Name -EQ $docName}
#store the file xml in an xml file named after the document
Write-FieldsAsXML $listFile.XML $fileXML

Now I have a single element XML file with all properties including managed metadata. 
#get the XML file and put the attributes and attr values in a hash table
[string]$fileFieldsXMLName = "$DownloadFileFolder\$docLibName-$docName.xml".toString()
[xml]$fileFieldsXML = Get-Content $fileFieldsXMLName
$fileFields = @{}
$fileFieldsXML.row.Attributes | foreach{$fileFields[([string]$_.Name)] = ([string]$_.value)}
#SPFileCollection.Add(String,FileStream,HashTable,Boolean)

[Microsoft.SharePoint.SPFile]$spFile = $folder.Files.Add([string]$uploadURL, [System.IO.Stream]$fileStream,$fileFields, $true)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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