SharePoint Online: ค้นหาการใช้งานประเภทเนื้อหาโดยใช้ PowerShell
ความต้องการ: ค้นหาตำแหน่งที่ใช้ประเภทเนื้อหาใน SharePoint Online
ต้องการรับข้อมูลการใช้งานสำหรับชนิดเนื้อหาใน SharePoint Online หรือไม่ ในโพสต์บล็อกนี้ เราจะแสดงวิธีค้นหาวิธีใช้ประเภทเนื้อหาใน SharePoint Online โดยใช้ PowerShell คุณอาจต้องค้นหาการใช้งานประเภทเนื้อหาในสถานการณ์เช่น:
- คุณได้รับข้อผิดพลาด "ประเภทเนื้อหายังคงใช้งานอยู่" เมื่อพยายามลบประเภทเนื้อหา
- คุณกำลังพยายามพิจารณาว่ามีการใช้เนื้อหาประเภทใดประเภทหนึ่ง ฯลฯ
PowerShell เพื่อค้นหาการใช้งานประเภทเนื้อหาใน SharePoint Online
ชนิดเนื้อหาจะอธิบายข้อมูลเมตาและลักษณะการทำงานของรายการในรายการหรือไลบรารี ไม่เหมือนกับ SharePoint On-premises เราไม่มีคลาส SPContentTypeUsage ใน CSOM ดังนั้นเราจึงเหลือตัวเลือก: สแกนแต่ละรายการเพื่อรับการใช้งานประเภทเนื้อหา!
#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 to get the content type usage
Function Find-SPOContentTypeUsage([String]$SiteURL)
{
Try{
Write-host -f Yellow "Processing Site:" $SiteURL
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get All Lists of the Web
$Ctx.Load($Ctx.Web)
$Ctx.Load($Ctx.Web.Lists)
$Ctx.Load($ctx.Web.Webs)
$Ctx.ExecuteQuery()
#Get content types of each list from the web
$ContentTypeUsages=@()
ForEach($List in $Ctx.Web.Lists)
{
$ContentTypes = $List.ContentTypes
$Ctx.Load($ContentTypes)
$Ctx.Load($List.RootFolder)
$Ctx.ExecuteQuery()
#Get List URL
If($Ctx.Web.ServerRelativeUrl -ne "/")
{
$ListURL= $("{0}{1}" -f $Ctx.Web.Url.Replace($Ctx.Web.ServerRelativeUrl,''), $List.RootFolder.ServerRelativeUrl)
}
else
{
$ListURL= $("{0}{1}" -f $Ctx.Web.Url, $List.RootFolder.ServerRelativeUrl)
}
#Get each content type data
ForEach($CType in $ContentTypes)
{
$ContentTypeUsage = New-Object PSObject
$ContentTypeUsage | Add-Member NoteProperty SiteURL($SiteURL)
$ContentTypeUsage | Add-Member NoteProperty ListName($List.Title)
$ContentTypeUsage | Add-Member NoteProperty ListURL($ListURL)
$ContentTypeUsage | Add-Member NoteProperty ContentTypeName($CType.Name)
$ContentTypeUsages += $ContentTypeUsage
}
}
#Export the result to CSV file
$ContentTypeUsages | Export-CSV $ReportOutput -NoTypeInformation -Append
#Iterate through each subsite of the current web and call the function recursively
foreach ($Subweb in $Ctx.web.Webs)
{
#Call the function recursively to process all subsites underneaththe current web
Find-SPOContentTypeUsage($Subweb.url)
}
}
Catch {
write-host -f Red "Error Generating Content Type Usage Report!" $_.Exception.Message
}
}
#Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$ReportOutput ="C:\Temp\ContentTypeUsage.csv"
#Get Credentials to connect
$Cred= Get-Credential
#Delete the Output Report, if exists
if (Test-Path $ReportOutput) { Remove-Item $ReportOutput }
#Call the function to get the content type usage
Find-SPOContentTypeUsage $SiteURL
นี่คือตัวอย่างเอาต์พุตของสคริปต์
สคริปต์ PowerShell นี้รับชนิดเนื้อหาทั้งหมดของรายการและไลบรารีทั้งหมดในคอลเลกชันไซต์ SharePoint Online จะทำอย่างไรถ้าคุณต้องการค้นหาว่ามีการใช้ประเภทเนื้อหาเฉพาะที่ใดบ้าง
SharePoint Online: ค้นหาตำแหน่งที่ใช้ประเภทเนื้อหาเฉพาะโดยใช้ PowerShell
ตอนนี้ เรามาค้นหาเอกสารทั้งหมดที่มีประเภทเนื้อหาใน SharePoint Online โดยใช้ PowerShell กัน ต่อไปนี้เป็นวิธีรับการใช้งานชนิดเนื้อหาใน 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"
#Function to get a particular content type usage
Function Get-SPOContentTypeUsage([String]$SiteURL, [String]$ContentTypeName)
{
Try{
Write-host -f Yellow "Processing Site:" $SiteURL
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get All Lists of the Web
$Ctx.Load($Ctx.Web)
$Ctx.Load($Ctx.Web.Lists)
$Ctx.Load($ctx.Web.Webs)
$Ctx.ExecuteQuery()
#Get content types of each list from the web
$ContentTypeUsages=@()
ForEach($List in $Ctx.Web.Lists)
{
$ContentTypes = $List.ContentTypes
$Ctx.Load($ContentTypes)
$Ctx.Load($List.RootFolder)
$Ctx.ExecuteQuery()
#Get List URL
If($Ctx.Web.ServerRelativeUrl -ne "/")
{
$ListURL= $("{0}{1}" -f $Ctx.Web.Url.Replace($Ctx.Web.ServerRelativeUrl,''), $List.RootFolder.ServerRelativeUrl)
}
else
{
$ListURL= $("{0}{1}" -f $Ctx.Web.Url, $List.RootFolder.ServerRelativeUrl)
}
#Get each content type data
ForEach($CType in $ContentTypes)
{
If($CType.Name -eq $ContentTypeName)
{
$ContentTypeUsage = New-Object PSObject
$ContentTypeUsage | Add-Member NoteProperty SiteURL($SiteURL)
$ContentTypeUsage | Add-Member NoteProperty ListName($List.Title)
$ContentTypeUsage | Add-Member NoteProperty ListURL($ListURL)
$ContentTypeUsage | Add-Member NoteProperty ContentTypeName($CType.Name)
$ContentTypeUsages += $ContentTypeUsage
Write-host -f Green "Found the Content Type at:" $ListURL
}
}
}
#Export the result to CSV file
$ContentTypeUsages | Export-CSV $ReportOutput -NoTypeInformation -Append
#Iterate through each subsite of the current web and call the function recursively
foreach ($Subweb in $Ctx.web.Webs)
{
#Call the function recursively to process all subsites underneaththe current web
Get-SPOContentTypeUsage $Subweb.url $ContentTypeName
}
}
Catch {
write-host -f Red "Error Generating Content Type Usage Report!" $_.Exception.Message
}
}
#Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$ReportOutput ="C:\Temp\ContentTypeUsage.csv"
$ContentTypeName="Business Contacts"
#Get Credentials to connect
$Cred= Get-Credential
#Delete the Output Report, if exists
if (Test-Path $ReportOutput) { Remove-Item $ReportOutput }
#Call the function to get the content type usage
Get-SPOContentTypeUsage $SiteURL $ContentTypeName
PowerShell นี้ค้นหาไฟล์ที่มีประเภทเนื้อหาใน SharePoint Online และยังช่วยให้คุณทราบตำแหน่งทั้งหมดของการใช้งานประเภทเนื้อหาเฉพาะอีกด้วย
PnP PowerShell เพื่อรับการใช้งานประเภทเนื้อหาในไซต์ SharePoint Online
#Parameters
$SiteURL="https://crescent.sharepoint.com/sites/PMO"
$ReportOutput ="C:\Temp\ContentTypeUsage.csv"
$ContentTypeName="Crescent Project V2"
#Delete the Output Report, if exists
If (Test-Path $ReportOutput) { Remove-Item $ReportOutput }
Try{
#Connect to the Site
Connect-PnPOnline -Url $SiteURL -Interactive
#Get All Lists
$Lists = Get-PnPList -Includes RootFolder | Where-Object {$_.Hidden -eq $False}
#Get content types of each list from the web
$ContentTypeUsages=@()
ForEach($List in $Lists)
{
Write-host -f Yellow "Scanning List:" $List.Title
$ListURL = $List.RootFolder.ServerRelativeUrl
#get all content types from the list
$ContentType = Get-PnPContentType -List $List | Where {$_.Name -eq $ContentTypeName}
#Collect list details
If($ContentType)
{
$ContentTypeUsage = New-Object PSObject
$ContentTypeUsage | Add-Member NoteProperty SiteURL($SiteURL)
$ContentTypeUsage | Add-Member NoteProperty ListName($List.Title)
$ContentTypeUsage | Add-Member NoteProperty ListURL($ListURL)
$ContentTypeUsage | Add-Member NoteProperty ContentTypeName($ContentType.Name)
Write-host -f Green "`tFound the Content Type in Use!"
#Export the result to CSV file
$ContentTypeUsage | Export-CSV $ReportOutput -NoTypeInformation -Append
}
}
}
Catch {
write-host -f Red "Error Generating Content Type Usage Report!" $_.Exception.Message
}
โปรดทราบว่าคุณไม่สามารถลบชนิดเนื้อหาได้ตราบเท่าที่มีการใช้งานชนิดเนื้อหานั้นบนไซต์ SharePoint ใดๆ นอกจากนี้ เมื่อต้องการเอาชนิดเนื้อหาออกจากไซต์ SharePoint รายการใดๆ ที่ใช้ชนิดเนื้อหานั้นจะต้องเปลี่ยนชนิดเนื้อหาหรือถูกเอาออกทั้งหมด (แม้จะมาจากถังรีไซเคิลขั้นที่ 1 และขั้นที่ 2)
ต่อไปนี้เป็นอีกโพสต์หนึ่งเพื่อค้นหาว่ามีการใช้ประเภทเนื้อหาที่ใดใน SharePoint On-premises: ค้นหาการใช้งานประเภทเนื้อหาใน SharePoint โดยใช้ PowerShell
