Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PowerShell将带有元数据的文件上载到SharePoint Online_Powershell_Sharepoint - Fatal编程技术网

使用PowerShell将带有元数据的文件上载到SharePoint Online

使用PowerShell将带有元数据的文件上载到SharePoint Online,powershell,sharepoint,Powershell,Sharepoint,我正在尝试使用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.ContentSt

我正在尝试使用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()
@瓦迪姆格雷米切夫——谢谢!问题。。。我上传了一个CSV文件。CSV列之一是元数据标记,我正在使用哈希将其从termstore转换为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()

谢谢

谢谢,这么简单!我所需要做的就是添加
$item=$upload.ListItemAllFields
,没错,你们非常接近
ListItemAllFields
属性返回关联的列表item@VadimGremyachev我的文档库已启用版本。我使用的是上面相同的代码,但我知道如何在文件上传完成后创建两个主要版本。第一个是普通文件,第二个显示元数据值更改。这是正常行为吗?@AsadRefai我认为这是一种预期行为,因为提交了两个查询1)创建文件操作2)更新列表item@VadimGremyachev谢谢