2016-01-22 86 views
3

我試圖使用PowerShell將一批文件上傳到SharePoint Online,並且也包括元數據(列字段)。我知道如何上傳文件,這工作得很好:使用PowerShell將文件上傳到SharePoint Online使用元數據

$fs = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open) 
$fci= New-Object Microsoft.SharePoint.Client.FileCreationInformation 
$fci.Overwrite = $true 
$fci.ContentStream = $fs 
$fci.URL = $file 
$upload = $list.RootFolder.Files.Add($fci) 
$ctx.Load($upload) 
$ctx.ExecuteQuery() 

,我知道如何編輯的字段/列,這個工程:

... 
$item["project"] = "Test Project" 
$item.Update() 
... 
$list.Update() 
$ctx.ExecuteQuery() 

,但我不知道如何配合兩個在一起。我需要獲取對已上傳文件的項目引用,以便我可以更新項目/文件的元數據。你可以猜到,PowerShell和SharePoint對我來說都是新的!

回答

3

下面的例子演示瞭如何在PowerShell中使用SharePoint CSOM API上傳文件,並設置文件的元數據:

$filePath = "C:\Users\jdoe\Documents\SharePoint User Guide.docx" #source file path 
$listTitle = "Documents" 
$targetList = $Context.Web.Lists.GetByTitle($listTitle) #target list 

#1.Upload a file 
$fci= New-Object Microsoft.SharePoint.Client.FileCreationInformation 
$fci.Overwrite = $true 
$fci.Content = [System.IO.File]::ReadAllBytes($filePath) 
$fci.URL = [System.IO.Path]::GetFileName($filePath) 
$uploadFile = $targetList.RootFolder.Files.Add($fci) 

#2.Set metadata properties 
$listItem = $uploadFile.ListItemAllFields 
$listItem["LastReviewed"] = [System.DateTime]::Now 
$listItem.Update() 

$Context.Load($uploadFile) 
$Context.ExecuteQuery() 
+1

謝謝,太簡單了!我所需要做的就是添加'$ item = $ upload.ListItemAllFields' – Mateus

+1

沒錯,你非常接近。 'ListItemAllFields'屬性返回一個關聯的列表項 –

+0

@VadimGremyachev我的文檔庫啓用了版本。我在上面使用相同的代碼,但是一些文件上傳完成後我會創建2個主要版本。首先是正常的文件,第二個是顯示元數據值的變化。這是一個正常的行爲? –

0

@vadimGremyachev ---謝謝!問題...我有一個CSV文件,我正在上傳。其中一個CSV列是元數據標記,我正在使用哈希將其轉換爲termstore中的GUID。在500個文件中,我有~5個拒絕設置SPO值的文件。它不會拋出錯誤。我知道我的散列中的GUID是正確的,因爲其他文檔使用HASH中的共享值進行標記。

#2.Set metadata properties 
    $listItem = $upload.ListItemAllFields 
    $listItem["LegacySharePointFolder"] = $row.LegacySharePointFolder 
    $listItem.Update() 
    $listItem["Customer"] = $TermStoreCustomerHash[$row.CUSTOMER] 
    $listItem.Update() 

謝謝!

相關問題