如何使用 PnP PowerShell 修改内容类型中的字段属性?

How to modify field properties in content type using PnP PowerShell?

我需要使用 PnP PowerShell 将一列的验证从 optional 更改为 required 仅在特定内容类型中。

我找到了 Set-PnPField 命令,但不确定如何使用它来仅更新特定内容类型的属性。以下是我到目前为止编写的代码:

$FieldName = "<Internal Name of the Field>"
$ListName = "<Name of the List>"
            
# connect to the portal using client id and client secret key
Connect-PnPOnline -Url <SharePoint Site Collecton URL> -ClientId <Client ID> -ClientSecret <Client Secret Key>

# get the current web
$spWeb = Get-PnPWeb -ErrorAction SilentlyContinue

if($spWeb -ne $null)
{
    #Get the Field from List
    $Field = Get-PnPField -List $ListName -Identity $FieldName -ErrorAction Stop
    #Set the Field Required
    $Field.Required = $True
    $Field.Update()
    $Field.Context.ExecuteQuery()
}

此代码仅在列表级别更新字段验证。如果有任何方法可以满足要求,请帮助我。提前致谢。

下面一个对我有用 - 它更新了内容类型:

# Get Context
$clientContext = Get-PnPContext
 
# Give target content type name over here
$targetContentType = Get-PnPContentType -Identity "ContentTypeName"
 
# Load target content type and content type fields
$clientContext.Load($targetContentType)
$clientContext.Load($targetContentType.Fields)
 
$clientContext.ExecuteQuery()
 
# Get required field from the target content type
# Mention field internal name or display name over here
$targetField = $targetContentType.Fields.GetByInternalNameOrTitle("TestColumnName")
 
$clientContext.Load($targetField)
 
$clientContext.ExecuteQuery()
 
# Make content type field required
$targetContentType.FieldLinks.GetById($targetField.Id).Required = 1
 
# Update(UpdateChildren – bool), this value indicates whether the children content type(inheriting from this Content Type) needs to be updated. 0 = False, 1 = True
$targetContentType.Update(0)
 
$clientContext.ExecuteQuery()
 
Disconnect-PnPOnline