繁体   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