SharePoint Online: PowerShell kullanarak İçerik Türü kullanımını bulma


Gereksinim: SharePoint Online'da İçerik Türünün nerede kullanıldığını bulun.

SharePoint Online'daki içerik türlerine ilişkin kullanım bilgilerini almak ister misiniz? Bu blog yazısında, PowerShell kullanarak SharePoint Online'da içerik türlerinin nasıl kullanıldığını nasıl öğreneceğinizi göstereceğiz. İçerik türü kullanımını aşağıdaki gibi senaryolarda bulmanız gerekebilir:

  • Bir içerik türünü silmeye çalıştığınızda “içerik türü hala kullanımda” hatası alıyorsunuz.
  • Belirli bir içerik türünün nerede kullanıldığını vb. belirlemeye çalışıyorsunuz.

SharePoint Online'da İçerik Türü Kullanımını Bulmak için PowerShell

İçerik türü, bir liste veya kitaplıktaki öğelerin meta verilerini ve davranışını açıklar. SharePoint On-premises'ın aksine, CSOM'da SPContentTypeUsage sınıfımız yoktur. Böylece elimizde bir seçenek kalıyor: İçerik türü kullanımını öğrenmek için her listeyi tarayın!


#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

İşte betiğin örnek çıktısı.

Bu PowerShell betiği, bir SharePoint Online site koleksiyonundaki tüm listelerin ve kitaplıkların tüm içerik türlerini alır. Belirli bir içerik türünün nerede kullanıldığını bulmak isterseniz ne olur?

SharePoint Online: PowerShell Kullanılarak Belirli Bir İçerik Türünün Nerede Kullanıldığını Bulun

Şimdi PowerShell kullanarak SharePoint Online’da içerik türüne sahip tüm belgeleri bulalım. SharePoint Online'da içerik türü kullanımının nasıl elde edileceği aşağıda açıklanmıştır.


#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 

Bu PowerShell, SharePoint Online'da içerik türlerine sahip dosyaları bulur ve ayrıca belirli içerik türü kullanımının tüm konumlarını da size getirir.

PnP PowerShell, SharePoint Online Sitesinde İçerik Türü Kullanımı Sağlayacak


#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
}

İçerik türü herhangi bir SharePoint sitesinde kullanıldığı sürece içerik türünü silemeyeceğinizi lütfen unutmayın. Ayrıca, SharePoint sitesinden bir içerik türünü kaldırmak için, belirli içerik türünü kullanan herhangi bir öğenin ya içerik türünü değiştirmesi ya da tamamen kaldırılması gerekir (1. aşama ve 2. aşama geri dönüşüm kutularından bile).

SharePoint Şirket İçi'nde bir içerik türünün nerede kullanıldığını bulmak için başka bir gönderi: PowerShell kullanarak SharePoint'te İçerik Türü Kullanımını Bulma

İlgili makaleler