SharePoint Online: Tạo cột ngày và giờ trong danh sách bằng PowerShell
Làm cách nào để thêm cột ngày và giờ vào danh sách SharePoint Online?
Cột Ngày và Giờ trong SharePoint Online thu thập các giá trị ngày và thời gian tùy chọn. Để thêm cột Ngày và Giờ vào danh sách, hãy làm theo các bước sau:
- Duyệt đến trang SharePoint Online của bạn, rồi dẫn hướng đến danh sách mục tiêu mà bạn muốn tạo cột ngày và giờ.
- Trong tab Danh sách, nhấp vào nút “Tạo cột” trong ruy-băng.
- Cung cấp Tên cho cột mới của bạn và chỉ định loại là “Ngày và giờ”.
Điền vào các giá trị tùy chọn khác và nhấp vào “OK” để tạo cột ngày và giờ trong danh sách SharePoint Online.
Đây là tập lệnh PowerShell để tạo cột ngày và giờ cho danh sách hoặc thư viện SharePoint Online.
PowerShell để thêm trường ngày và giờ trong danh sách SharePoint Online
Dưới đây là cách sử dụng PowerShell để thêm cột ngày và giờ vào danh sách 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-DateTimeColumnToList()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $Name,
[Parameter(Mandatory=$true)] [string] $DisplayName,
[Parameter(Mandatory=$true)] [string] $Description,
[Parameter(Mandatory=$true)] [string] $Format,
[Parameter(Mandatory=$true)] [string] $IsRequired,
[Parameter(Mandatory=$true)] [string] $FriendlyDisplayFormat,
[Parameter(Mandatory=$true)] [string] $EnforceUniqueValues
)
#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
{
#Define XML for Field Schema
$FieldSchema = "<Field Type='DateTime' ID='{$FieldID}' Name='$Name' StaticName='$Name' DisplayName='$DisplayName' Format='$Format' Required='$IsRequired' Description='$Description' EnforceUniqueValues='$EnforceUniqueValues' FriendlyDisplayFormat='$FriendlyDisplayFormat' />"
$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="ProjectStartDate" #Column Name
$DisplayName="Project Start Date"
$Description="Enter the Project Start Date"
$Format="DateOnly" #DateTime
$IsRequired = "FALSE"
$EnforceUniqueValues="FALSE"
$FriendlyDisplayFormat="Disabled" #Relative
#Call the function to add column to list
Add-DateTimeColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -IsRequired $IsRequired -Format $Format -EnforceUniqueValues $EnforceUniqueValues -FriendlyDisplayFormat $FriendlyDisplayFormat -Description $Description
PnP PowerShell để thêm cột ngày và giờ vào danh sách SharePoint Online
Để thêm cột Ngày và Giờ, bạn có thể sử dụng lệnh ghép ngắn Add-PnPField. Đây là một ví dụ về PowerShell:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName= "Projects"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Add Date Time Field to list
Add-PnPField -List $ListName -Type DateTime -DisplayName "Start Date" -InternalName "StartDate" -AddToDefaultView
Tương tự, chúng ta có thể tạo cột Ngày giờ từ lược đồ XML:
#Define XML Schema for Date Field
$FieldXML= "<Field Type='DateTime' Name='StartDate' ID='$([GUID]::NewGuid())' DisplayName='Start Date' Required ='FALSE' Format='DateOnly' FriendlyDisplayFormat='Disabled'></Field>"
#Add DateTime Field to list
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName
Bằng cách làm theo các bước được nêu trong bài viết này, bạn có thể dễ dàng tạo cột Ngày và Giờ trong SharePoint Online để theo dõi dữ liệu ngày và giờ. Bạn có thể thêm cột Ngày và Giờ vào danh sách SharePoint Online bằng cả giao diện trình duyệt web và PowerShell.
