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

如何通过PowerShell将现有People Picker列添加到SharePoint内容类型

问题根因

你当前脚本混用了SharePoint服务器端对象模型和**CSOM(客户端对象模型)**的API:

  • 通过Get-SPWeb获取的SPWeb对象下的字段、内容类型都属于服务器端对象(Microsoft.SharePoint命名空间下的类型,比如你拿到的用户字段是Microsoft.SharePoint.SPFieldUser)
  • 后续创建字段关联时使用的Microsoft.SharePoint.Client.FieldLinkCreationInformation是CSOM的类型,要求传入CSOM的Field对象,两类对象不能直接转换,因此触发类型报错。
  • 另外原脚本中存在未定义变量$SiteColumnName,会导致运行时提示变量不存在,需要修正。
解决方案:使用纯服务器端对象模型修改(适配本地SharePoint环境)

把创建字段关联的逻辑替换为服务器端API的写法,修改后的完整可运行脚本如下:

$url = "xyz"
$Web = Get-SPWeb $url
AddColumnToContentType "Username" "CTLog"
$Web.Dispose() # 用完释放SPWeb对象避免内存泄漏

function AddColumnToContentType($columnname, $contenttypename){
    $ContentTypeColl = $Web.ContentTypes
    $ContentType = $ContentTypeColl | Where {$_.Name -eq $contenttypename}
    If($ContentType -eq $Null)
    {
        Write-host "Content Type '$ContentTypeName' doesn't exist" -f Yellow
        Return
    }
    $SiteColumnColl = $Web.Fields
    $SiteColumn = $SiteColumnColl | Where {$_.Title -eq $columnname}
    if($SiteColumn -eq $Null)
    {
        Write-host "Site Column '$columnname' doesn't exists!" -f Yellow
        Return
    }
    $Field = $ContentType.Fields | Where {$_.Title -eq $columnname}
    if($Field -ne $Null)
    {
        Write-host "Site Column '$columnname' Already Exists in the content type!" -f Yellow
        Return
    }
    # 替换为服务器端字段关联创建逻辑
    $FieldLink = New-Object Microsoft.SharePoint.SPFieldLink($SiteColumn)
    $ContentType.FieldLinks.Add($FieldLink)
    $ContentType.Update($true)
    Write-Host "Site column '$columnname' added to content type '$contenttypename' successfully" -f Green
}
运行要求
  • 该脚本需要在SharePoint本地服务器上,以场管理员权限运行SharePoint Management Shell执行
  • 如果你操作的是SharePoint Online或者需要远程执行,需要改为纯CSOM的写法,全程使用CSOM相关命令,不要混用Get-SPWeb这类服务器端命令。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 22:18:03