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

[玩转系统] SharePoint Online:使用 PowerShell 在列表视图中添加/删除字段

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

SharePoint Online:使用 PowerShell 在列表视图中添加/删除字段


要求:SharePoint Online PowerShell 更新列表视图。

如何在 SharePoint Online 列表视图中添加或删除列?

SharePoint Online 列表视图使您能够以最适合您需求的方式排列信息。您可以自定义 SharePoint Online 视图以添加或删除其中的字段。例如,您可能有一个包含数十列的列表,并且您可能只需要在日常工作中查看这些列的子集。这篇博文将了解如何使用 PowerShell 在视图中添加或删除字段。如果您需要快速关注大型列表中的特定数据,这会很方便。

要在 SharePoint Online 列表视图中添加或删除列,请执行以下步骤:

  1. 导航到您的 SharePoint Online 列表 >> 从“视图”下拉列表中单击“编辑当前视图”。

    [玩转系统] SharePoint Online:使用 PowerShell 在列表视图中添加/删除字段

  2. 在“修改视图”页面上,根据您的要求选择/取消选择列旁边的复选框

    [玩转系统] SharePoint Online:使用 PowerShell 在列表视图中添加/删除字段

  3. 单击“保存”以提交您的更改。您还可以通过选择列设置>>从任何列下拉列表中显示/隐藏列来显示或隐藏视图中的列。

    [玩转系统] SharePoint Online:使用 PowerShell 在列表视图中添加/删除字段

  4. 只需选择/取消选择任何字段即可使其在列表视图中可见或隐藏。

    [玩转系统] SharePoint Online:使用 PowerShell 在列表视图中添加/删除字段

现在,让我们使用 PowerShell 将列添加到 SharePoint Online 视图。

SharePoint Online:PowerShell 添加要查看的字段

以下是用于将字段添加到 SharePoint Online 中的现有列表视图的 PowerShell:


#Load SharePoint Online 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"
   
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
$ListName="Documents"
$FieldToAdd="ID"
$UserName="[email protected]"
$Password ="Password goes here"

Try {
    
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

    #Get Web information and subsites
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $credentials
    
    #get the list
    $List = $Context.web.lists.GetByTitle($Listname)
    
    #Check if View has the field already
    $ViewFields = $List.DefaultView.ViewFields
    # Get a specific list view by Name
    #$view=$List.Views.getByTitle($ViewName)
    $Context.load($ViewFields)  
    $Context.ExecuteQuery()  

    if( ($ViewFields -Contains $FieldToAdd) -eq $false) {
        #Add the Field to View
        $List.DefaultView.ViewFields.Add($FieldToAdd)
        $List.DefaultView.Update()
        $Context.ExecuteQuery()

        Write-host "List View updated!" -ForegroundColor Green 
    }
    else { 
    write-host "Field exists in the view already!" -foregroundcolor Red 
    }

}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

上面的脚本将“ID”字段添加到给定库的默认视图中

使用 PnP PowerShell 将列添加到列表视图:

让我向您展示如何使用 PnP PowerShell 将字段添加到 SharePoint Online 列表视图。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName= "Projects"
$ViewName= "Active Projects"
$ColumnName = "Category" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #Get the Context
    $Context = Get-PnPContext

    #Get the List View from the list
    $ListView  =  Get-PnPView -List $ListName -Identity $ViewName -ErrorAction Stop

    #Check if view doesn't have the column already
    If($ListView.ViewFields -notcontains $ColumnName)
    {
        #Add Column to View
        $ListView.ViewFields.Add($ColumnName)
        $ListView.Update()
        $Context.ExecuteQuery()
        Write-host -f Green "Column '$ColumnName' Added to View '$ViewName'!"
    }
    else
    {
        Write-host -f Yellow "Column '$ColumnName' Already Exists in View '$ViewName'!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

在 SharePoint Online 中使用 PowerShell 从列表视图中删除字段:

同样,要从视图中删除字段,PowerShell 脚本如下所示:


#Load SharePoint Online 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"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
$ListName="Documents"
$FieldToAdd="ID"
$UserName="[email protected]"
$Password ="Password goes here"

Try {    
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $credentials
    
    #get the list
    $List = $Context.web.lists.GetByTitle($Listname)
    
    #Check if the View has the field in it
    $ViewFields = $List.DefaultView.ViewFields
    $Context.load($ViewFields)  
    $Context.ExecuteQuery()  

    if( ($ViewFields -Contains $FieldToAdd) -eq $true) {
        #Add the Field to View
        $List.DefaultView.ViewFields.Remove($FieldToAdd)
        $List.DefaultView.Update()
        $Context.ExecuteQuery()

        Write-host "Field Has been removed from the List View!" -ForegroundColor Green 
    }
    else { 
    write-host "Field doesn't exist in the view!" -foregroundcolor Red 
    }

}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

使用 PnP PowerShell 从 SharePoint Online 视图中删除列:

让我们使用 PnP PowerShell 从视图中删除列。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName= "Projects"
$ViewName= "Active Projects"
$ColumnName = "Category" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #Get the Context
    $Context = Get-PnPContext

    #Get the List View from the list
    $ListView  =  Get-PnPView -List $ListName -Identity $ViewName -ErrorAction Stop

    #Check if view doesn't have the column already
    If($ListView.ViewFields -contains $ColumnName)
    {
        #Add Column to View
        $ListView.ViewFields.Remove($ColumnName)
        $ListView.Update()
        $Context.ExecuteQuery()
        Write-host -f Green "Column '$ColumnName' Removed from View '$ViewName'!"
    }
    else
    {
        Write-host -f Yellow "Column '$ColumnName' doesn't exist in View '$ViewName'!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

如果您需要 SharePoint Online PowerShell 来更新 ListView,例如视图筛选器,请参阅:SharePoint Online:PowerShell 来更新视图

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

取消回复欢迎 发表评论:

关灯