เพิ่ม-ลบประเภทเนื้อหาในรายการ SharePoint หรือไลบรารีโดยใช้ PowerShell
ชนิดเนื้อหา SharePoint ช่วยให้ผู้ใช้สามารถจัดระเบียบข้อมูลที่เกี่ยวข้องร่วมกันได้ ทุกอย่างถูกสร้างขึ้นตามชนิดเนื้อหาใน SharePoint เพื่อให้มีความสอดคล้องและสามารถนำกลับมาใช้ใหม่ได้ ตัวอย่างเช่น เทมเพลตรายการทั้งหมด เช่น ประกาศและงานถูกสร้างขึ้นด้วยประเภทเนื้อหา “ประกาศ” และ “งาน” ตามลำดับ! โดยทั่วไปแล้ว ประเภทเนื้อหาจะประกอบด้วยเทมเพลตและคอลัมน์ข้อมูลเมตา เช่น ลองพิจารณาประเภทเนื้อหา "เอกสารข้อเสนอการขาย" ซึ่งอาจสร้างขึ้นที่ไซต์คอลเลกชันพร้อมเทมเพลตสำหรับข้อเสนอการขายและคอลัมน์เพื่อบันทึกข้อมูลเมตาที่เกี่ยวข้อง และสามารถเชื่อมโยงกับไลบรารีเอกสารจำนวนเท่าใดก็ได้
ตามค่าเริ่มต้น ชนิดเนื้อหาจะถูกกำหนดขอบเขตที่ระดับไซต์คอลเลกชัน เมื่อคุณเพิ่มชนิดเนื้อหาลงในรายการ SharePoint ชนิดเนื้อหานั้นจะถูกคัดลอกจากแกลเลอรีชนิดเนื้อหาของไซต์ไปยังรายการ ซึ่งหมายความว่าเมื่อคุณกำหนดชนิดเนื้อหาเองในระดับรายการ จะไม่ส่งผลกระทบต่อชนิดเนื้อหาหลักซึ่งอยู่ที่ ระดับไซต์คอลเลกชัน
วิธีการเชื่อมโยงประเภทเนื้อหากับไลบรารีหรือรายการ SharePoint
เมื่อต้องการเพิ่มชนิดเนื้อหาลงในรายการหรือไลบรารีใน SharePoint ให้ทำดังต่อไปนี้:
- ไปที่การตั้งค่ารายการ/ไลบรารี
- คลิกที่การตั้งค่าขั้นสูงภายใต้กลุ่มการตั้งค่าทั่วไป
- ในตัวเลือกประเภทเนื้อหา ให้ตั้งค่าเป็น "ใช่" เพื่ออนุญาตการจัดการประเภทเนื้อหา
- ตอนนี้ ในหน้าการตั้งค่ารายการ คุณจะพบส่วนใหม่ "ประเภทเนื้อหา" พร้อมด้วยประเภทเนื้อหาที่เกี่ยวข้องทั้งหมดในรายการ
คุณสามารถคลิกลิงก์ "เพิ่มจากประเภทเนื้อหาไซต์ที่มีอยู่" เพื่อเพิ่มประเภทเนื้อหาที่มีอยู่ลงในรายการได้
-
บนหน้า "เพิ่มประเภทเนื้อหา" ในส่วน "ประเภทเนื้อหาของไซต์ที่มีอยู่" ให้เลือกประเภทเนื้อหาที่คุณต้องการเพิ่มลงในรายการหรือไลบรารี คลิกเพิ่ม จากนั้นคลิกตกลง ตอนนี้ประเภทเนื้อหาใหม่จะปรากฏบนหน้าการตั้งค่า
เพิ่มชนิดเนื้อหาลงในรายการ SharePoint โดยใช้ PowerShell:
หากคุณต้องการเชื่อมโยงประเภทเนื้อหากับหลายรายการบนหลายไซต์ คุณสามารถใช้สคริปต์ PowerShell ด้านล่างเพื่อทำสิ่งนี้โดยทางโปรแกรม! ในกรณีของฉัน ฉันมีเว็บไซต์ที่มีไซต์ย่อยมากกว่า 50 แห่ง ข้อกำหนดคือการเพิ่มประเภทเนื้อหาลงในไลบรารีเฉพาะบนไซต์ทั้งหมด ดังนั้นฉันจึงเขียนฟังก์ชันแบบกำหนดเองของ PowerShell นี้เพื่อเพิ่มประเภทเนื้อหาที่ถูกลบในไลบรารี SharePoint
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Custom PowerShell Function to Add Content type to SharePoint list
Function Add-ContentTypeToList([Microsoft.SharePoint.SPList]$List, $ContentTypeName)
{
#Check if Content Type Management is enabled
if($List.ContentTypesEnabled -ne $true)
{
Write-Host "Enabling Content type management on list: $($list.Title)"
$List.ContentTypesEnabled = $true
$List.Update()
}
#Get the content type
$ContentType = $List.ParentWeb.Site.RootWeb.ContentTypes[$ContentTypeName]
#If content type found in the site
if ($ContentType)
{
#Check If list doesn't has the specific content type already
if (-not $list.ContentTypes[$ContentTypeName])
{
#Add content type to the list
$list.ContentTypes.Add($ContentType) | Out-Null
$list.Update()
Write-Host "Content Type Added to the list: $($List.Title)"
}
else
{
Write-Host "Content Type '$($ContentTypeName)' already exists on the list '$($list.Title)'"
}
}
else
{
Write-Host "Cannot find the Content Type '$($ContentTypeName)' to add to list '$($list.Title)'"
}
}
#Variables for processing
$WebURL ="https://sales.crescent.com"
$ListName ="Q1 Sales Proposal"
$ContentTypeName ="Sales Proposal"
#Get the Web and List
$Web = Get-SPWeb $WebURL
$List = $Web.lists.TryGetList($ListName)
if($list -ne $null)
{
#Call the function to add content type
Add-ContentTypeToList $list $ContentTypeName
}
จะลบประเภทเนื้อหาออกจากรายการได้อย่างไร?
หากต้องการลบประเภทเนื้อหาที่ไม่ได้ใช้ที่คุณไม่ได้ใช้ ให้ทำดังต่อไปนี้:
- เรียกดูการตั้งค่ารายการหรือหน้าการตั้งค่าไลบรารี
- ใต้ส่วน "ประเภทเนื้อหา" คลิกประเภทเนื้อหาที่คุณต้องการลบ
- คลิกลิงก์ "ลบประเภทเนื้อหานี้" จากนั้นคลิกตกลงที่พร้อมท์การยืนยัน
ลบชนิดเนื้อหาออกจากรายการ SharePoint โดยใช้ PowerShell
ใช้สคริปต์ PowerShell นี้เพื่อลบชนิดเนื้อหาออกจากรายการ SharePoint หรือไลบรารีเอกสาร:
#Custom PowerShell Function to Remove Content type from SharePoint list
Function Remove-ContentTypeFromList([Microsoft.SharePoint.SPList]$List, $ContentTypeName)
{
#Check if Content Type Management is enabled
if($List.ContentTypesEnabled -ne $true)
{
Write-Host "Enabling Content type management on list: $($list.Title)"
$List.ContentTypesEnabled = $true
$List.Update()
}
#Get the content to remove from list
$ContentType = $List.ContentTypes[$ContentTypeName]
#If content type found in the site
if ($ContentType)
{
#Check If list doesn't has the specific content type already
if ($list.ContentTypes[$ContentTypeName])
{
#Remove content type from the list
$list.ContentTypes.Delete($ContentType.Id)
$list.Update()
Write-Host "Content Type has been removed from the list: $($List.Title)"
}
else
{
Write-Host "Content Type '$($ContentTypeName)' doesn't exists on the list '$($list.Title)'"
}
}
else
{
Write-Host "Cannot find the Content Type '$($ContentTypeName)' to add to list '$($list.Title)'"
}
}
#Variables for processing
$WebURL ="https://sales.crescent.com"
$ListName ="Q1 Sales Proposal"
$ContentTypeName ="Sales Proposal"
#Get the Web and List
$Web = Get-SPWeb $WebURL
$List = $Web.lists.TryGetList($ListName)
if($list -ne $null)
{
#remove content type from list, call:
Remove-ContentTypeFromList $list $ContentTypeName
}
คุณไม่สามารถลบประเภทเนื้อหาได้หากมีการสร้างรายการตามประเภทเนื้อหาแล้ว! หากคุณพยายามลบประเภทเนื้อหาที่ใช้งานอยู่ คุณจะพบข้อผิดพลาด “ประเภทเนื้อหายังคงใช้งานอยู่”!

