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

[玩转系统] 使用 PowerShell 将列添加到 SharePoint 中的视图

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

使用 PowerShell 将列添加到 SharePoint 中的视图


如何添加要在 SharePoint 中查看的列?

只需几个简单的步骤即可将列添加到视图中。要在 SharePoint 视图中添加列,请执行以下步骤:

  • 浏览到 SharePoint 中的列表或库 >> 单击功能区中的“列表”或“库”选项卡。
  • 从“当前视图”下拉列表中选择适当的视图。默认视图通常会列在当前视图中。单击“管理视图”组下的“修改视图”按钮。
  • 在“编辑视图”页面上,选择您希望在特定视图中显示的列。

    [玩转系统] 使用 PowerShell 将列添加到 SharePoint 中的视图

  • 单击“确定”保存。这会将列添加到列表视图。

PowerShell 添加要查看的字段:

如何使用 PowerShell 将列添加到 SharePoint 列表视图?让我们以编程方式添加一个自定义列以使用 PowerShell 进行查看。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#configuration parameters
$WebURL="https://portal.crescent.com/projects"
$ListName="Project Milestones"
$ViewName="All Items"
$FieldInternalName="ProjectDescription"

Function Add-FieldToView([Microsoft.SharePoint.SPList]$List, [String]$ViewName, [string]$FieldInternalName)
{
    #Get the view
    $View = $List.Views[$ViewName]
    #To Get the Default View: List.DefaultView
    
    if($view -eq $Null) {write-host "View doesn't exists!" -f Red; return}
    
    #Check if view has the specific field already!
    if(!$view.ViewFields.ToStringCollection().Contains($FieldInternalName))
    {
        $View.ViewFields.Add($FieldInternalName)
        #To Delete a field from view: $View.ViewFields.delete($FieldInternalName)
        $View.Update()
        write-host "Field added to View!" -f Green
    }
    else
    {
        write-host "Field Already Exists in the view!" -f Red
    }
}

#Get the Web and List
$Web= Get-SPWeb $WebURL
$List = $web.Lists.TryGetList($ListName)

#If List Exists
if ($List )
{
    #Call the function
    Add-FieldToView $List $ViewName $FieldInternalName
} 

SharePoint PowerShell 将列添加到默认视图:

要将列添加到默认视图,请在上面的脚本中代替:


$View = $List.Views[$ViewName]

使用:


$View = $List.DefaultView

使用 PowerShell 的所有列到列表中的所有视图:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function Add-FieldToAllViews([Microsoft.SharePoint.SPList]$List, [string]$FieldInternalName)
{
    #Iterate through each view from the list
    For($i = $List.Views.Count-1; $i -ge 0; $i--)
    {
        $View = $List.Views[$i]
        #Check if view has the specific field already!
        if(!$view.ViewFields.ToStringCollection().Contains($FieldInternalName))
        {
            $View.ViewFields.Add($FieldInternalName)
            $View.Update()
            write-host "Field added to View:"$View.Title  -f Green
        }
        else
        {
            write-host "Field Already Exists in the view:"$View.Title  -f Yellow
        }
    }
}

#Parameters
$WebURL="https://intranet.crescent.com/"
$ListName="Documents"
$FieldInternalName="OverallProgress"

#Get the Web and List
$Web= Get-SPWeb $WebURL
$List = $web.Lists.TryGetList($ListName)

#If List Exists
if ($List )
{
    #Call the function to add field to all list views
    Add-FieldToAllViews $List $FieldInternalName
}

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

取消回复欢迎 发表评论:

关灯