You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Add-PnPFile向SharePoint库添加文件时日期列空值问题求助

解决SharePoint日期列空值导致元数据丢失的问题

当使用Add-PnPFile命令时,若日期类型字段传递空字符串,会导致所有元数据列变为空白。这是因为SharePoint的日期字段不接受空字符串作为有效值,触发了元数据解析错误。

解决方法

针对空的日期值,有两种可行处理方式:

1. 将空值替换为$null

# 预处理日期字段,空值替换为$null
$effectiveDate = [string]::IsNullOrEmpty($Values[3]) ? $null : $Values[3]
$datePublished = [string]::IsNullOrEmpty($Values[4]) ? $null : $Values[4]
$endDate = [string]::IsNullOrEmpty($Values[5]) ? $null : $Values[5]

# 执行文件添加命令
Add-PnPFile -Path $Path -Folder $LibraryName -Values @{
    "Name" = $Values[0]
    "Classification" = $Values[1]
    "DocumentID" = $Values[2]
    "EffectiveDate" = $effectiveDate
    "DatePublished" = $datePublished
    "EndDate" = $endDate
    "RNumber" = $Values[6]
}

2. 移除空值对应的字段键

仅在日期值非空时,才将该字段加入元数据哈希表:

# 初始化基础元数据
$metadata = @{
    "Name" = $Values[0]
    "Classification" = $Values[1]
    "DocumentID" = $Values[2]
    "RNumber" = $Values[6]
}

# 非空日期字段才添加到元数据
if (-not [string]::IsNullOrEmpty($Values[3])) {
    $metadata["EffectiveDate"] = $Values[3]
}
if (-not [string]::IsNullOrEmpty($Values[4])) {
    $metadata["DatePublished"] = $Values[4]
}
if (-not [string]::IsNullOrEmpty($Values[5])) {
    $metadata["EndDate"] = $Values[5]
}

# 执行文件添加命令
Add-PnPFile -Path $Path -Folder $LibraryName -Values $metadata

原理说明

  • 传递$null时,PnP PowerShell会将其解析为SharePoint日期字段的合法空值,不会触发解析错误。
  • 移除空值字段的键,SharePoint会保持该字段的默认空状态,同时确保其他元数据正常应用。

内容的提问来源于stack exchange,提问作者DanielJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 06:28:14