Tìm kiếm trang web

SharePoint Online: Đặt giá trị mặc định cho cột danh sách bằng PowerShell


Yêu cầu: Cài đặt giá trị mặc định của Cột Đặt SharePoint Online.

Làm cách nào để đặt cài đặt giá trị mặc định của cột trong SharePoint Online?

Khi bạn tạo danh sách SharePoint, bạn chỉ định loại dữ liệu được lưu trữ trong mỗi cột. Tuy nhiên, nếu bạn muốn đặt giá trị mặc định cho một cột thì sao? Có thể bạn muốn đảm bảo rằng một giá trị cụ thể được tự động điền vào cột của bạn bất cứ khi nào một mục mới được thêm vào. Bài viết này sẽ hướng dẫn bạn cách đặt giá trị mặc định cho một cột trong danh sách SharePoint Online.

Để đặt trước giá trị mặc định cho một trường trong danh sách SharePoint Online,

  1. Đi tới Cài đặt danh sách >> Trong “Cột”, chọn trường bằng cách nhấp vào tiêu đề của nó.
  2. Trong phần “Giá trị mặc định”, bạn có thể chỉ định giá trị mặc định cho trường để nó được điền tự động mà không cần người dùng nhập.

Bạn cũng có thể chỉ định giá trị mặc định cho một cột tại thời điểm tạo!

Định cấu hình giá trị cột mặc định bằng PowerShell CSOM

Dưới đây là cách đặt giá trị mặc định cho một cột trong danh sách SharePoint Online bằng PowerShell:


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

#Function to set default value of a field
Function Set-SPOFieldDefaultValue($SiteURL, $ListName, $FieldInternalName, $DefaultValue)
{
    Try { 
        #Get Credentials to connect
        $Cred= Get-Credential
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Get the List
        $List=$Ctx.Web.Lists.GetByTitle($ListName)

        #Get the Field
        $Field = $List.Fields.GetByInternalNameOrTitle($FieldInternalName)
        $Field.DefaultValue = $DefaultValue
        $Field.Update()
        $Ctx.ExecuteQuery()
 
        Write-host -f Green "Default Value set for Field Successfully!"
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
#Set parameter values
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Team Documents"
$FieldInternalName = "Classification" #Internal Name
$DefaultValue = "Invoice; Proposal; Quotation"

Set-SPOFieldDefaultValue -SiteURL $SiteURL -ListName $ListName -FieldInternalName $FieldInternalName -DefaultValue $DefaultValue

Để đặt giá trị mặc định cho cột danh sách SharePoint Online, bạn có thể sử dụng PowerShell để cập nhật thuộc tính “DefaultValue” của trường. Tương tự, bạn có thể đặt giá trị mặc định của cột Ngày thành ngày Hôm nay là:


Set-SPOFieldDefaultValue -SiteURL "https://Crescent.sharepoint.com" -ListName "Tasks" -FieldInternalName "DueDate" -DefaultValue "[Today]"

SharePoint Online: Cài đặt giá trị mặc định của cột bằng PnP PowerShell

Cách PnP PowerShell để đặt giá trị mặc định cho cột danh sách SharePoint Online:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Team Documents"
$ColumnName = "Classification" #Internal Name
$DefaultValue = "Invoice; Proposal; Quotation"

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

#Set Default Value for the field
Set-PnPField -Identity $ColumnName -List $ListName -Values @{DefaultValue=$DefaultValue}

Và kết quả:

SharePoint Online: Đặt giá trị mặc định cho trường tra cứu

Để đặt giá trị mặc định cho cột tra cứu trong danh sách SharePoint Online, hãy sử dụng như sau:


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$FieldName = "ParentProject" #Internal Name

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Set Default Value for the Person column
$DefaultValue = "1;#OS Upgrade" #ID and Text of the Parent Item in the given format

Set-PnPField -Identity $FieldName -List $ListName -Values @{DefaultValue=$DefaultValue}

Bạn cũng có thể sử dụng lệnh ghép ngắn Set-PnPDefaultColumnValues để đặt giá trị mặc định cho một cột trong thư viện Tài liệu SharePoint Online.


Set-PnPDefaultColumnValues -List Documents -Field TaxKeyword -Value "Crescent|Projects|InfraBuild"

SharePoint Online: Đặt giá trị mặc định cho cột người hoặc nhóm

Đây là cách đặt giá trị mặc định cho cột Người trong Thư viện Tài liệu (Tuy nhiên, điều này không hoạt động đối với danh sách!):


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"
$ListName = "Documents"
$FieldName = "ProjectManager" #Internal Name
$DefaultUserID = "i:0#.f|membership|steve@crescent.com"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the User
$User = Get-PnPUser -Identity $DefaultUserID

#Set Default Value for the Person column
Set-PnPDefaultColumnValues -List $ListName -Field $FieldName -Value "$($User.Id);#$($User.LoginName)"

Bài viết liên quan