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

[玩转系统] SharePoint Online:如何使用 PowerShell 从列表中隐藏列?

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

SharePoint Online:如何使用 PowerShell 从列表中隐藏列?


要求:使用 PowerShell 隐藏 SharePoint Online 中的字段。

在项目请求列表中,当用户填写表单时,我们希望默认隐藏“状态”列,然后使用工作流程来填充特定请求的状态。同样,对于“编辑”列,但仅在“视图”表单上显示它。那么,如何从 SharePoint Online 列表中隐藏列呢?

SharePoint Online 使用 PowerShell 从列表中隐藏列:

以下是用于隐藏 SharePoint Online 列表中的列表列的 PowerShell 脚本。


#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"
  
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/prjects/"
$ListName="ProjectRequest"
$FieldName = "Status" #Display Name

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
  
    #Get the web, List and Field objects
    $Web=$Ctx.Web
    $List=$web.Lists.GetByTitle($ListName)
    $Field = $List.Fields.GetByTitle($FieldName)

    #Hide the column from New & Edit forms
    $Field.SetShowInEditForm($False) 
    $Field.SetShowInNewForm($False) 
    $Field.UpdateAndPushChanges($True)
    $Ctx.ExecuteQuery()
     
    Write-host -f Green "List Field hidden Successfully!"
}
Catch {
    write-host -f Red "Error hiding List Column: " $_.Exception.Message
}

此 PowerShell 隐藏列表的新表单和编辑表单中的特定字段!

[玩转系统] SharePoint Online:如何使用 PowerShell 从列表中隐藏列?

SharePoint Online:使用 PnP PowerShell 隐藏新项目表单中的列

使用此 PnP PowerShell 脚本隐藏 SharePoint Online 列表中的列:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName = "Projects"
$FieldName =  "Status"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Field
$Field = Get-PnPField -List $ListName | Where {$_.Title -eq $FieldName}

#Hide the field from New Form
$Field.SetShowInNewForm($False)
#$Field.SetShowInEditForm($True)
Invoke-PnPQuery

您还可以使用 Set-PnPField cmdlet 从 SharePoint Online 列表中隐藏字段:


#Connect to SharePoint Online
Connect-PnPOnline -Url "https://crescent.sharepoint.com/sites/marketing" -Interactive

#Hide a column
Set-PnPField -List "Config" -Identity "Title" -Values @{Required=$False;Hidden=$True}

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

取消回复欢迎 发表评论:

关灯