SharePoint Online: Thêm cột vào danh sách loại nội dung bằng PowerShell
Yêu cầu: Thêm trường để liệt kê loại nội dung trong SharePoint Online.
Làm cách nào để thêm cột vào loại Nội dung Danh sách hoặc Thư viện trong SharePoint Online?
PowerShell là ngôn ngữ kịch bản mạnh mẽ có thể giúp tự động hóa nhiều tác vụ trong SharePoint. Có thể đôi khi bạn cần thêm cột vào kiểu nội dung danh sách. Bạn có thể thực hiện việc này theo cách thủ công nhưng có thể tốn thời gian nếu bạn có nhiều danh sách và loại nội dung. Bài đăng blog này sẽ chỉ cho bạn cách thêm cột vào loại nội dung danh sách SharePoint Online bằng PowerShell.
Nếu bạn muốn thêm một trường vào kiểu nội dung của danh sách hoặc thư viện, hãy làm theo các bước sau:
- Đi tới cài đặt danh sách hoặc thư viện >> Nhấp vào tên loại nội dung phù hợp từ trang cài đặt danh sách.
- Trên trang Loại nội dung danh sách, trong phần cột, nhấp vào liên kết “Thêm từ trang web hiện có hoặc cột danh sách”
Chọn nhóm cột thích hợp, sau đó chọn cột bạn muốn thêm vào loại nội dung, nhấp vào nút “Thêm”, sau đó OK để thêm cột đó vào loại nội dung của bạn.
Bạn có thể thêm cột từ các cột của danh sách hiện có hoặc từ các cột của trang.
SharePoint Online: PowerShell để thêm trường vào loại nội dung danh sách
Hãy thêm một trường để liệt kê loại nội dung bằng PowerShell. Dưới đây là cách sử dụng PowerShell để thêm cột vào loại nội dung danh sách:
#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 Add-ColumnToListContentType()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $ContentTypeName,
[Parameter(Mandatory=$true)] [string] $ColumnName,
[Parameter(Mandatory=$false)] [bool] $IsSiteColumn=$True
)
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()
#Get the content type from list
$ContentTypeColl = $List.ContentTypes
$Ctx.Load($ContentTypeColl)
$Ctx.ExecuteQuery()
#Check if the content type exists in the list
$ContentType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
If($ContentType -eq $Null)
{
Write-host "Content Type '$ContentTypeName' doesn't exists in '$ListName'" -f Yellow
Return
}
#Get the column to add: Either site column or existing list's column
If($IsSiteColumn)
{
$ColumnColl = $Ctx.Web.Fields
}
else #List Column
{
$ColumnColl = $List.Fields
}
$Ctx.Load($ColumnColl)
$Ctx.ExecuteQuery()
$Column = $ColumnColl | Where {$_.Title -eq $ColumnName}
#Check if given column exists
if($Column -eq $Null)
{
Write-host "Column '$ColumnName' doesn't exists!" -f Yellow
Return
}
else
{
#Check if column already added to the content type
$FieldCollection = $ContentType.Fields
$Ctx.Load($FieldCollection)
$Ctx.ExecuteQuery()
$Field = $FieldCollection | Where {$_.Title -eq $ColumnName}
if($Field -ne $Null)
{
Write-host "Column '$ColumnName' Already Exists in the content type!" -f Yellow
Return
}
#Add field to content type
$FieldLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation
$FieldLink.Field = $Column
[Void]$ContentType.FieldLinks.Add($FieldLink)
$ContentType.Update($false)
$Ctx.ExecuteQuery()
Write-host "Column '$ColumnName' Added to '$ContentTypeName' Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Adding Column to Content Type!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ContentTypeName="Projects"
$ColumnName="Head Count"
$IsSiteColumn=$false
#Call the function
Add-ColumnToListContentType -SiteURL $SiteURL -ListName $ListName -ContentTypeName $ContentTypeName -ColumnName $ColumnName
Dựa trên giá trị tham số “IsSiteColumn”, tập lệnh này có thể thêm cột trang hoặc cột danh sách vào một loại nội dung nhất định.
PnP PowerShell để thêm cột vào danh sách loại nội dung trong SharePoint Online
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$ContentTypeName = "Crescent Project V2"
$FieldName = "Department"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the Content Type from the List
$ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName
#Add a column list
$Field = Add-PnPField -List $ListName -DisplayName $FieldName -InternalName $FieldName -Type Choice -Choices "Sales","Marketing","Purchase" -AddToDefaultView
#Add the column to Content type
Add-PnPFieldToContentType -Field $Field -ContentType $ContentType
}
Catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Nếu bạn cần thêm trường vào loại nội dung trang, hãy sử dụng: SharePoint Online: Cách thêm cột trang vào loại nội dung bằng PowerShell?
