เปลี่ยนประเภทเนื้อหาของรายการที่มีอยู่ใน SharePoint โดยใช้ PowerShell
ความต้องการ:
เรามีไลบรารีเอกสารที่มีอยู่ซึ่งมีเอกสารข้อเสนอการขายกว่า 100 รายการอยู่ในนั้น เพื่อบังคับใช้มาตรฐาน ทีมขายได้สร้างประเภทเนื้อหา "ข้อเสนอการขาย" เราได้รับข้อกำหนดในการเปลี่ยนประเภทเนื้อหาของเอกสารที่มีอยู่ทั้งหมดที่จัดเก็บไว้ในไลบรารีเอกสารเฉพาะด้วยประเภทเนื้อหาเริ่มต้น "เอกสาร" กล่าวอีกนัยหนึ่ง ขณะนี้เอกสารทั้งหมดจำเป็นต้องได้รับการกำหนดประเภทเนื้อหาใหม่ "ข้อเสนอการขาย"
จะเปลี่ยนประเภทเนื้อหาของรายการที่มีอยู่ (เอกสาร) ใน SharePoint ได้อย่างไร
คุณสามารถเปลี่ยนประเภทเนื้อหาของรายการที่มีอยู่ได้โดยเข้าไปที่หน้าแก้ไขคุณสมบัติของรายการนั้น ๆ และสลับช่อง "ประเภทเนื้อหา"
แม้ว่าการเปลี่ยนแปลงประเภทเนื้อหาของรายการที่มีอยู่ใน SharePoint จะค่อนข้างง่ายและตรงไปตรงมา แต่คุณไม่ต้องการดูเอกสารแต่ละฉบับในไลบรารีเอกสารและกำหนดประเภทเนื้อหาใหม่ด้วยตนเอง เนื่องจากจะใช้เวลานาน ใช่ไหม?
ให้เราใช้ PowerShell เพื่อให้มันง่ายและรวดเร็ว! สคริปต์นี้จะวนซ้ำเอกสารทั้งหมดที่จัดเก็บไว้ในไลบรารีเอกสารที่กำหนด ตรวจสอบประเภทเนื้อหาของแต่ละเอกสาร และหากเป็นประเภทเนื้อหาเฉพาะ สคริปต์จะแทนที่ประเภทเนื้อหาเก่าด้วยประเภทเนื้อหาใหม่
สคริปต์ PowerShell เพื่อเปลี่ยนประเภทเนื้อหาของรายการใน SharePoint
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Custom PowerShell Function to switch Content type of all items stored in a SharePoint library
Function Change-ItemContentType {
param(
[Microsoft.SharePoint.SPWeb]$Web = $(throw "Please provide the 'Web' Parameter!"),
[string]$ListName = $(throw "Please provide the 'ListName' Parameter!"),
[string]$OldContentTypeName = $(throw "Please provide the 'OldContentTypeName' Parameter!"),
[string]$NewContentTypeName = $(throw "Please provide the 'NewContentTypeName' Parameter!")
)
#Get the list
$List = $Web.Lists[$ListName]
#Get the new content type
$NewContentType = $List.ContentTypes[$NewContentTypeName]
#Checkin any checked out file
foreach($item in $list.Items)
{
if($item.File.CheckOutStatus -ne "None")
{
#check file in
$item.File.CheckIn("Checked In By Administrator")
write-host "Checked-out file checked-in on item: "$item.id
}
}
#Iterate through each item in the list
foreach($item in $list.Items)
{
if($item.ContentType.Name -eq $OldContentTypeName)
{
#Change the content type
$item.File.CheckOut()
$item["ContentTypeId"] = $NewContentType.Id
$item.SystemUpdate()
$item.File.CheckIn("Content type changed to " + $NewContentType.Name, 1)
Write-host "Content type changed for item: "$item.id
}
}
}
#Variables for processing
$WebURL ="https://sales.crescent.com"
$ListName ="Q1 Sales Proposals"
$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
Change-ItemContentType $web $list "document" "Sales Proposal"
}
การดำเนินการนี้จะเปลี่ยน “เอกสาร” ประเภทเนื้อหาเก่าของเอกสารทั้งหมดเป็นประเภทเนื้อหาใหม่ 'ข้อเสนอการขาย' ครั้งต่อไป เมื่อผู้ใช้แก้ไขเอกสาร พวกเขาจะได้รับแจ้งให้ป้อนข้อมูลในช่องที่ต้องกรอกจากประเภทเนื้อหาใหม่
นี่คือโพสต์อื่นสำหรับ SharePoint Online: PowerShell สำหรับ SharePoint Online เพื่อเปลี่ยนประเภทเนื้อหา
