当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 查找内容类型使用情况

作者:精品下载站 日期:2024-12-14 14:33:32 浏览:15 分类:玩电脑

SharePoint Online:使用 PowerShell 查找内容类型使用情况


要求:查找 SharePoint Online 中使用内容类型的位置。

想要获取 SharePoint Online 中内容类型的使用信息?那么,在这篇博文中,我们将向您展示如何使用 PowerShell 了解如何在 SharePoint Online 中使用内容类型。您可能需要在以下场景中找到内容类型的用法:

  • 尝试删除内容类型时,您收到“内容类型仍在使用中”错误。
  • 您正在尝试确定特定内容类型的使用位置等。

使用 PowerShell 查找 SharePoint Online 中的内容类型使用情况

内容类型描述列表或库中项目的元数据和行为。与 SharePoint 本地部署不同,我们在 CSOM 中没有可用的 SPContentTypeUsage 类。因此,我们只剩下一个选项:扫描每个列表以获取内容类型的使用情况!


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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

这是脚本的示例输出。

[玩转系统] SharePoint Online:使用 PowerShell 查找内容类型使用情况

此 PowerShell 脚本获取 SharePoint Online 网站集中所有列表和库的所有内容类型。如果您想查找使用特定内容类型的位置该怎么办?

SharePoint Online:使用 PowerShell 查找使用特定内容类型的位置

现在,让我们使用 PowerShell 查找 SharePoint Online 中具有某种内容类型的所有文档。以下是如何获取 SharePoint Online 中的内容类型使用情况。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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 网站中删除内容类型,任何使用特定内容类型的项目都必须更改其内容类型或完全删除(甚至从第一阶段和第二阶段回收站中删除)。

这是另一篇文章,用于查找 SharePoint 本地中使用内容类型的位置:使用 PowerShell 在 SharePoint 中查找内容类型用法

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯