SharePoint Online: Xóa Cột Trang khỏi Loại Nội dung bằng PowerShell
Yêu cầu: Xóa Cột Trang web khỏi Loại Nội dung trong SharePoint Online.
Làm cách nào để xóa cột khỏi loại nội dung trong SharePoint Online?
Trong bài đăng này, chúng tôi sẽ hướng dẫn bạn cách xóa cột khỏi loại nội dung trong SharePoint Online. Khi bạn loại bỏ một cột trang khỏi một kiểu nội dung, nó sẽ không xóa cột đó khỏi trang SharePoint Online. Nó chỉ đơn giản là loại bỏ liên kết của cột trang khỏi định nghĩa của loại nội dung cụ thể. Để loại bỏ một cột khỏi kiểu nội dung, hãy làm theo các bước sau:
- Đi tới Cài đặt trang >> Loại nội dung trang (Nếu ở cấp Danh sách, hãy đi tới Cài đặt danh sách).
- Chọn loại nội dung từ trang, Trong phần cột, Nhấp vào cột để xóa khỏi loại nội dung.
Trong trang loại nội dung thay đổi, hãy nhấp vào nút “Xóa” và xác nhận lời nhắc xóa cột khỏi loại nội dung.
SharePoint Online: Xóa Cột khỏi Loại Nội dung bằng PowerShell
Bạn có cần loại bỏ cột khỏi kiểu nội dung trong SharePoint Online không? PowerShell khiến mọi việc trở nên dễ dàng! Đây là cách xóa một cột khỏi loại nội dung 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 Remove-ColumnFromContentType()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ContentTypeName,
[Parameter(Mandatory=$true)] [string] $SiteColumnName
)
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 content type from web
$ContentTypeColl = $Ctx.Web.ContentTypes
$Ctx.Load($ContentTypeColl)
$Ctx.ExecuteQuery()
#Check if the content type exists in the site
$ContentType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
If($ContentType -eq $Null)
{
Write-host "Content Type '$ContentTypeName' doesn't exists in '$SiteURL'" -f Yellow
Return
}
#Get the column to delete from content type
$ContentTypeFieldColl = $ContentType.Fields
$Ctx.Load($ContentTypeFieldColl)
$Ctx.ExecuteQuery()
$SiteColumn = $ContentTypeFieldColl | Where {$_.Title -eq $SiteColumnName }
if($SiteColumn -eq $null)
{
Write-host "Site Column '$SiteColumnName' doesn't exists in content Type '$ContentTypeName'" -f Yellow
}
else
{
#Get the field link from content type
$FieldLinkColl = $ContentType.FieldLinks
$Ctx.Load($FieldLinkColl)
#Remove field from content type
$FieldLink = $FieldLinkColl.GetById($SiteColumn.Id)
$FieldLink.DeleteObject()
$ContentType.Update($true)
$Ctx.ExecuteQuery()
Write-host "Site Column '$SiteColumnName' deleted from '$ContentTypeName' successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Deleting Site Column from Content Type!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ContentTypeName="Project Template"
$SiteColumnName="Project Department" #Display Name of the Field
#Call the function
Remove-ColumnFromContentType -SiteURL $SiteURL -ContentTypeName $ContentTypeName -SiteColumnName $SiteColumnName
PnP PowerShell để xóa cột khỏi loại nội dung trong SharePoint Online
Nếu bạn muốn loại bỏ một cột khỏi kiểu nội dung trong SharePoint Online, bạn có thể sử dụng PnP PowerShell! Tôi sẽ chỉ cho bạn một ví dụ về cách sử dụng PowerShell để xóa một cột khỏi loại nội dung.
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ContentTypeName ="Crescent Invoice Template V2"
$FieldName ="Project_x0020_Manager"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get the Client Context
$Context = Get-PnPContext
#Get the content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName
If($ContentType)
{
#Get the Field from Content type
$Field = $ContentType.Fields.GetByInternalNameOrTitle($FieldName)
$Context.Load($Field)
$Context.ExecuteQuery()
#Remove the Field from content type
$ContentType.FieldLinks.GetById($Field.Id).DeleteObject()
$ContentType.Update($False) #Update children
$Context.ExecuteQuery()
Write-host -f Green "Field '$FieldName' has been removed from Content Type!"
}
Ngoài ra, bạn có thể sử dụng lệnh ghép ngắn Remove-PnPFieldFromContentType trong PnP PowerShell. Đây là cách thực hiện:
Remove-PnPFieldFromContentType -Field $FieldName -ContentType $ContentTypeName
