Búsqueda de sitios web

SharePoint Online: agregar y quitar campos a la vista de lista usando PowerShell


Requisito: SharePoint Online PowerShell para actualizar una vista de lista.

¿Cómo agregar o eliminar columnas en la vista de lista de SharePoint Online?

La vista de lista de SharePoint Online le permite organizar la información de la forma que mejor se adapte a sus necesidades. Puede personalizar una vista de SharePoint Online para agregarle o eliminar campos. Por ejemplo, es posible que tenga una lista con docenas de columnas y que solo necesite ver un subconjunto de esas columnas en su trabajo diario. Esta publicación de blog aprenderá cómo agregar o eliminar campos de la vista usando PowerShell. Esto puede resultar útil si necesita centrarse rápidamente en datos específicos de una lista grande.

Para agregar o eliminar columnas en la vista de lista de SharePoint Online, siga estos pasos:

  1. Navegue a su lista de SharePoint Online >> Haga clic en "Editar vista actual" en el menú desplegable Ver.

  2. En la página Modificar vista, seleccione o desmarque las casillas de verificación junto a las columnas según sus requisitos.

  3. Haga clic en Guardar para confirmar sus cambios. También puede mostrar u ocultar columnas de una vista seleccionando Configuración de columna >> Mostrar/ocultar columnas en cualquier menú desplegable de columnas.

  4. Simplemente seleccione/deseleccione cualquier campo para hacerlo visible u ocultarlo de la vista de lista.

Ahora, agreguemos una columna a la vista de SharePoint Online usando PowerShell.

SharePoint Online: PowerShell para agregar un campo para ver

Aquí está el PowerShell para agregar un campo a una vista de lista existente en SharePoint Online:


#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
$ListName="Documents"
$FieldToAdd="ID"
$UserName="Salaudeen@crescent.com"
$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
}

El script anterior agrega el campo "ID" a la vista predeterminada de la biblioteca dada

Agregar columna a la vista de lista usando PnP PowerShell:

Permítame mostrarle cómo agregar un campo a su vista de lista de SharePoint Online usando 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 -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
}

Eliminar campo de la vista de lista usando PowerShell en SharePoint Online:

De manera similar, para eliminar un campo de la vista, el script de PowerShell es el siguiente:


#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
$ListName="Documents"
$FieldToAdd="ID"
$UserName="Salaudeen@crescent.com"
$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
}

Eliminar columna de la vista de SharePoint Online usando PnP PowerShell:

Usemos PnP PowerShell para eliminar una columna de la vista.


#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
}

Si necesita SharePoint Online PowerShell para actualizar ListView, como Ver filtro, consulte: SharePoint Online: PowerShell para actualizar la vista

Artículos relacionados