SharePoint Online:使用 PowerShell 查找内容类型中的网站栏使用情况
要求:在 SharePoint Online 网站集中的内容类型中查找网站列使用情况。
一点背景知识:当尝试删除 SharePoint Online 中的网站栏时,收到一条错误消息:“无法删除内容类型中包含的网站栏。在删除此网站栏之前先删除对此网站栏的所有引用”。
那么,如何找到网站栏被使用的地方呢?
PowerShell 查找内容类型中的网站栏使用情况:
此 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 to get a particular Site column usage in content types
Function Get-SPOSiteColumnUsage([String]$SiteURL, [String]$SiteColumnInternalName)
{
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 Content Types of the Web
$Ctx.Load($Ctx.Web)
$Ctx.Load($Ctx.Web.Webs)
$ContentTypes = $Ctx.web.ContentTypes
$Ctx.Load($ContentTypes)
$Ctx.ExecuteQuery()
#Leave Hidden content types
$ContentTypes = $ContentTypes | Where {$_.Group -ne "_Hidden"}
Write-host -f Yellow "`t Found $($ContentTypes.count) Content Types"
#Loop through each content types Field
ForEach($ContentType in $ContentTypes)
{
Write-host -f Yellow "`t `t Scanning Content Type $($ContentType.Name)"
$Ctx.Load($ContentType.Fields)
$Ctx.ExecuteQuery()
ForEach($Field in $ContentType.Fields)
{
If($Field.InternalName -eq $SiteColumnInternalName)
{
Write-host -f Green "`t `t Found the Site Column in Content Type '$($ContentType.Name)' at Site '$($SiteURL)'"
}
}
}
#Iterate through each subsite of the current web
foreach ($Subweb in $Ctx.web.Webs)
{
#Call the function recursively
Get-SPOSiteColumnUsage $Subweb.url $SiteColumnInternalName
}
}
Catch {
write-host -f Red "Error Generating Site Column Usage Report!" $_.Exception.Message
}
}
#Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$SiteColumnInternalName="TotalInvestment"
#Get Credentials to connect
$Cred= Get-Credential
#Call the function to get the content type usage
Get-SPOSiteColumnUsage $SiteURL $SiteColumnInternalName
该脚本扫描所有内容类型并打印结果:

