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

SharePoint Online: เพิ่มคอลัมน์ตัวเลขลงในรายการโดยใช้ PowerShell


ข้อกำหนด: สร้างคอลัมน์ตัวเลขในรายการ SharePoint Online

จะเพิ่มคอลัมน์ตัวเลขในรายการ SharePoint Online ได้อย่างไร

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

  1. เรียกดูไซต์ SharePoint Online ของคุณและนำทางไปยังรายการเป้าหมายที่คุณต้องการเพิ่มคอลัมน์ตัวเลข
  2. ใต้แท็บรายการคลิกที่ปุ่ม "สร้างคอลัมน์" ใน Ribbon
  3. ระบุชื่อให้กับคอลัมน์ใหม่ของคุณและระบุประเภทเป็น "หมายเลข"

  4. เลื่อนลงและกรอกค่าตัวเลือกอื่นๆ เช่น คอลัมน์ที่จำเป็น, ไม่ซ้ำกัน, ต่ำสุดและสูงสุด, จำนวนตำแหน่งทศนิยม, ค่าเริ่มต้น ฯลฯ แล้วคลิก "ตกลง" เพื่อสร้างคอลัมน์ตัวเลขในรายการ SharePoint Online

PowerShell เพื่อสร้างคอลัมน์ตัวเลขเพื่อแสดงรายการใน SharePoint Online

ต่อไปนี้เป็นวิธีสร้างคอลัมน์รายการโดยใช้ PowerShell ใน SharePoint Online:


#Load SharePoint CSOM 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"

#Custom function to add column to list
Function Add-NumberColumnToList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $Name,
        [Parameter(Mandatory=$true)] [string] $DisplayName,
        [Parameter(Mandatory=$false)] [string] $Description=[string]::Empty,
        [Parameter(Mandatory=$false)] [string] $IsRequired = "FALSE",
        [Parameter(Mandatory=$false)] [string] $EnforceUniqueValues = "FALSE"
    )

    #Generate new GUID for Field ID
    $FieldID = New-Guid

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()

        #Check if the column exists in list already
        $Fields = $List.Fields
        $Ctx.Load($Fields)
        $Ctx.executeQuery()
        $NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
        if($NewField -ne $NULL)  
        {
            Write-host "Column $Name already exists in the List!" -f Yellow
        }
        else
        {
            $FieldSchema = "<Field Type='Number' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' />"
            $NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
            $Ctx.ExecuteQuery()    

            Write-host "New Column Added to the List Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error Adding Column to List!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$Name="HeadCount"
$DisplayName="Head Count"
$Description="Enter the Number of Team Members in the Project"

#Call the function to add column to list
Add-NumberColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description 

หากต้องการเพิ่มพารามิเตอร์เพิ่มเติม เช่น ต่ำสุด สูงสุด เปอร์เซ็นต์ และทศนิยม ให้ผนวกด้านล่างเข้ากับ Schema XML:


#Load SharePoint CSOM 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"

#Custom function to add column to list
Function Add-NumberColumnToList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $Name,
        [Parameter(Mandatory=$true)] [string] $DisplayName,
        [Parameter(Mandatory=$false)] [string] $Description=[string]::Empty,
        [Parameter(Mandatory=$false)] [string] $IsRequired = "FALSE",
        [Parameter(Mandatory=$false)] [string] $EnforceUniqueValues = "FALSE",
        [Parameter(Mandatory=$true)] [string] $Min,
        [Parameter(Mandatory=$true)] [string] $Max,
        [Parameter(Mandatory=$true)] [string] $Percentage,
        [Parameter(Mandatory=$true)] [string] $Decimals
    )

    #Generate new GUID for Field ID
    $FieldID = New-Guid

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()

        #Check if the column exists in list already
        $Fields = $List.Fields
        $Ctx.Load($Fields)
        $Ctx.executeQuery()
        $NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
        if($NewField -ne $NULL)  
        {
            Write-host "Column $Name already exists in the List!" -f Yellow
        }
        else
        {
            $FieldSchema = "<Field Type='Number' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' Min='$Min' Max='$Max' Percentage='$Percentage' Decimals='$Decimals' />"
            $NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
            $Ctx.ExecuteQuery()    

            Write-host "New Column Added to the List Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error Adding Column to List!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$Name="Probability"
$DisplayName="Probability"
$Description="Enter the Probability Percentage"
$Min="0"
$Max="1" #100%
$Percentage="TRUE"
$Decimals="2"

#Call the function to add column to list
Add-NumberColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description -Min $Min -Max $Max -Percentage $Percentage -Decimals $Decimals

PnP PowerShell เพื่อเพิ่มคอลัมน์ตัวเลขในรายการ SharePoint Online

การเพิ่มคอลัมน์ลงในรายการ SharePoint Online สามารถทำได้โดยใช้ PnP PowerShell เช่นกัน


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

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

#Add Number Field to list
Add-PnPField -Type Number -List $ListName -DisplayName "Head Count" -InternalName "HeadCount"

นี่จะเป็นการเพิ่มคอลัมน์ใหม่ที่เรียกว่า "จำนวนหัว" ลงในรายการของคุณ คุณยังสามารถระบุตัวเลือกอื่นๆ เช่น คำอธิบาย จำเป็น ค่าเริ่มต้น และอื่นๆ ในทำนองเดียวกัน เราสามารถสร้างคอลัมน์ตัวเลขจาก XML schema ได้เช่นกัน


#Define XML Schema for Number Field
$FieldXML= "<Field Type='Number' Name='HeadCount' ID='$([GUID]::NewGuid())' DisplayName='Head Count' Required ='FALSE' Min='1' Max='50' ></Field>"

#Add number Field to list from XML
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName

ตั้งค่าแอตทริบิวต์ Percentage='TRUE' เพื่อทำเครื่องหมายฟิลด์เป็นเปอร์เซ็นต์!

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