ค้นหาเว็บไซต์

SharePoint Online: เพิ่ม-ลบฟิลด์ลงในมุมมองรายการโดยใช้ PowerShell


ข้อกำหนด: SharePoint Online PowerShell เพื่ออัปเดตมุมมองรายการ

จะเพิ่มหรือลบคอลัมน์ในมุมมองรายการ SharePoint Online ได้อย่างไร

มุมมองรายการ SharePoint Online ช่วยให้คุณสามารถจัดเรียงข้อมูลในลักษณะที่เหมาะสมกับความต้องการของคุณมากที่สุด คุณสามารถกำหนดมุมมอง SharePoint Online เองเพื่อเพิ่มหรือเอาเขตข้อมูลออกได้ เช่น คุณอาจมีรายการที่มีคอลัมน์หลายสิบคอลัมน์ และคุณอาจต้องดูเพียงส่วนย่อยของคอลัมน์เหล่านั้นในการทำงานในแต่ละวันของคุณ โพสต์ในบล็อกนี้จะได้เรียนรู้วิธีเพิ่มหรือลบฟิลด์ออกจากมุมมองโดยใช้ PowerShell สิ่งนี้มีประโยชน์หากคุณต้องการเน้นไปที่ข้อมูลเฉพาะในรายการขนาดใหญ่อย่างรวดเร็ว

เมื่อต้องการเพิ่มหรือลบคอลัมน์ในมุมมองรายการ SharePoint Online ให้ทำตามขั้นตอนเหล่านี้:

  1. นำทางไปยังรายการ SharePoint Online ของคุณ >> คลิกที่ "แก้ไขมุมมองปัจจุบัน" จากเมนูแบบเลื่อนลงมุมมอง

  2. บนหน้าแก้ไขมุมมอง เลือก/ยกเลิกการเลือกกล่องกาเครื่องหมายที่อยู่ถัดจากคอลัมน์ตามความต้องการของคุณ

  3. คลิกที่บันทึกเพื่อยืนยันการเปลี่ยนแปลงของคุณ คุณยังสามารถแสดงหรือซ่อนคอลัมน์จากมุมมองได้โดยเลือกการตั้งค่าคอลัมน์ >> แสดง/ซ่อนคอลัมน์จากดรอปดาวน์คอลัมน์ใดก็ได้

  4. เพียงเลือก/ยกเลิกการเลือกฟิลด์ใดๆ เพื่อให้มองเห็นได้หรือซ่อนจากมุมมองรายการ

ตอนนี้ มาเพิ่มคอลัมน์ลงในมุมมอง SharePoint Online โดยใช้ PowerShell

SharePoint Online: PowerShell เพื่อเพิ่มฟิลด์เพื่อดู

นี่คือ PowerShell เพื่อเพิ่มเขตข้อมูลลงในมุมมองรายการที่มีอยู่ใน 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
}

สคริปต์ด้านบนเพิ่มฟิลด์ "ID" ให้กับมุมมองเริ่มต้นของไลบรารีที่กำหนด

เพิ่มคอลัมน์ลงในมุมมองรายการโดยใช้ 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 -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
}

ลบฟิลด์ออกจากมุมมองรายการโดยใช้ PowerShell ใน SharePoint Online:

ในทำนองเดียวกัน หากต้องการลบฟิลด์ออกจากมุมมอง สคริปต์ PowerShell จะเป็นดังนี้:


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

ลบคอลัมน์ออกจากมุมมองออนไลน์ของ SharePoint โดยใช้ PnP PowerShell:

ลองใช้ 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 เช่น View Filter โปรดดูที่: SharePoint Online: PowerShell เพื่ออัปเดตมุมมอง

บทความที่เกี่ยวข้อง