【问题标题】:How to modify field properties in content type using PnP PowerShell?如何使用 PnP PowerShell 修改内容类型中的字段属性?
【发布时间】:2021-10-19 09:09:46
【问题描述】:

我需要使用 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()
}

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

【问题讨论】:

    标签: powershell sharepoint sharepoint-online


    【解决方案1】:

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

    # 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
    

    【讨论】:

      猜你喜欢
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 2019-07-28
      相关资源
      最近更新 更多