How can I upload a file to a library with PowerShell and maintain the metadata already associated with the file?

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/188283

Вопрос

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

Это было полезно?

Решение

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)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top