当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 应用列格式

作者:精品下载站 日期:2024-12-14 21:13:39 浏览:15 分类:玩电脑

SharePoint Online:使用 PowerShell 应用列格式


要求:使用 PowerShell 设置 SharePoint Online 中的列格式。

如何使用 PowerShell 在 SharePoint Online 列中应用条件格式?

SharePoint Online 中的列格式允许用户自定义列表或库中列的外观。我的另一篇文章中解释了 SharePoint Online 中的列格式设置:SharePoint Online 中的格式列。虽然应用列格式可以手动完成,但 PowerShell 可以自动化该过程。在本指南中,您将了解如何使用 PowerShell 在 SharePoint Online 中应用列格式。

以下是用于在 SharePoint Online 中设置列格式的 PowerShell CSOM 脚本:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"   
$ListName="Config"
$FieldName="Status" #Internal Name
$JsonFormat = @"
{
  "`$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "attributes": {
   "class": "=if(@currentField == 'Completed', 'sp-field-severity--good', if(@currentField == 'In Progress', 'sp-field-severity--low', if(@currentField == 'On-Hold','sp-field-severity--warning', if(@currentField == 'Not Started','sp-field-severity--blocked', ''))))"
  }
}
"@

#Get Credentials to connect
$Cred= Get-Credential
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Get the List
$List= $Ctx.Web.Lists.GetByTitle($ListName)
 
#Get the Field
$Field=$List.Fields.GetByInternalNameOrTitle($FieldName)
$Ctx.Load($Field)
$Ctx.ExecuteQuery()

#Apply Column Formatting to the field
$Field.CustomFormatter= $JsonFormat
$Field.Update()
$Ctx.ExecuteQuery()

PnP PowerShell 在 SharePoint Online 中设置列格式

我们还可以使用 PnP PowerShell cmdlet Set-PnPField 应用 JSON 列格式,如下所示:


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName="Config"
$FieldName="Status" #Internal Name
$JsonFormat = @"
{
  "`$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "attributes": {
   "class": "=if(@currentField == 'Completed', 'sp-field-severity--good', if(@currentField == 'In Progress', 'sp-field-severity--low', if(@currentField == 'On-Hold','sp-field-severity--warning', if(@currentField == 'Not Started','sp-field-severity--blocked', ''))))"
  }
}
"@

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive 
 
#Get the Field
$Field =  Get-PnPField -Identity $FieldName -List $ListName

#Set Custom Format
$Field | Set-PnPField -Values @{CustomFormatter = $JsonFormat}

这是结果:

[玩转系统] SharePoint Online:使用 PowerShell 应用列格式

总之,与手动方法相比,通过使用 PowerShell 在 SharePoint Online 中应用列格式,您可以节省时间和精力。 PowerShell 脚本可以轻松自定义 SharePoint 列表和库中列的外观。无论您是要添加颜色编码、更改字体大小还是应用条件格式,PowerShell 都可以帮助您实现目标。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯