【问题标题】:Upload file to SharePoint Online with metadata using PowerShell使用 PowerShell 将文件上传到带有元数据的 SharePoint Online
【发布时间】:2016-04-29 09:06:06
【问题描述】:

我正在尝试使用 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


    【解决方案1】:

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

    【讨论】:

    • 谢谢,这么简单!我需要做的就是添加$item = $upload.ListItemAllFields
    • 没错,你很接近。 ListItemAllFields 属性返回一个关联的列表项
    • @VadimGremyachev 我的文档库已启用版本。我使用的是上面相同的代码,但是一旦文件上传完成,我如何获得 2 个主要版本。首先是普通文件,其次是显示元数据值的变化。这是正常行为吗?
    • @AsadRefai 我认为这是可以预期的行为,因为提交了两个查询 1)创建文件操作 2)更新列表项
    • @VadimGremyachev 谢谢
    【解决方案2】:

    @vadimGremyachev --- 谢谢!问题... 我有一个我正在上传的文件的 CSV。 CSV 列之一是元数据标记,我使用哈希从术语库转换为它的 GUID。在 500 个文件中,我有约 5 个文件拒绝在 SPO 中设置值。它不会引发错误。我知道我的哈希中的 GUID 是正确的,因为其他文档被标记为哈希中的共享值。

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

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-28
      • 1970-01-01
      • 2021-09-25
      • 2021-09-25
      • 2021-09-25
      • 2017-02-11
      • 2021-12-29
      • 2020-05-12
      相关资源
      最近更新 更多