簡體   English   中英

使用PowerShell使用元數據將文件上載到SharePoint Online

[英]Upload file to SharePoint Online with metadata using PowerShell

我正在嘗試使用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對我來說都是新手!

以下示例演示了如何使用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()

@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()

謝謝!

您可以在不增加版本的情況下向現有記錄添加元數據。 #2.設置元數據屬性

    $listItem = $upload.ListItemAllFields

item.File.CheckOut();

$listItem["LegacySharePointFolder"] = $row.LegacySharePointFolder
$listItem.Update()
$listItem["Customer"] = $TermStoreCustomerHash[$row.CUSTOMER]
$listItem.Update()

item.File.CheckIn("",CheckinType.OverwriteCheckIn);

clientContext.ExecuteQuery();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM