script to get list of contant type from SC

sns 9,246 Reputation points
2021-09-06T10:17:59.647+00:00

is there any script to generate content type GUID along with list name, content type name and sub site name for the entire site collection?
If yes please share , output should be exported to excel

Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Elsie Lu_MSFT 9,801 Reputation points
    2021-09-07T09:08:49.733+00:00

    Hi @sns ,

    To loop through all the subsites:

    #Load SharePoint Online 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"  
         
    ##Variables for Processing  
    $SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"  
    $UserName="******@crescent.com"  
    $Password ="Password goes here"  
        
    #Setup Credentials to connect  
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))  
        
    #sharepoint online powershell get subsites  
    Function Get-SPOWeb() {  
    param(  
        $WebURL = $(throw "Please Enter the Site Collection URL")  
    )  
       
        #Get Web information and subsites  
        $Context = New-Object Microsoft.SharePoint.Client.ClientContext($webURL)  
        $Context.Credentials = $credentials  
        $Web = $context.Web  
        $Context.Load($web)  
       
        #powershell cmdlet to retrieve sharepoint online subsites  
        $Context.Load($web.Webs)  
        $Context.executeQuery()  
       
        #Do something with the current sub-site  
        Write-host $Web.URL  
       
        #Iterate through each subsite in the current web  
        foreach ($Subweb in $web.Webs)  
        {  
            #Call the function recursively to process all subsites underneath the current web  
            Get-SPOWeb($Subweb.url)  
        }  
    }  
       
    #Call the function  
    Get-SPOWeb -WebURL $SiteUrl  
    

    Test result:
    129823-56.jpg
    Reference:
    SharePoint Online: PowerShell to Get All Subsites in a Site Collection

    ===========================

    To loop through all the lists:

    Try{  
    Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.dll'  
    Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.Runtime.dll'  
    }  
    catch {  
    }  
    $siteUrl = "https://xxx.sharepoint.com/sites/TeamMisTest2"  
    $username = "******@xxx.onmicrosoft.com"  
    $password=ConvertTo-SecureString "xxxx" -AsPlainText -Force  
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)  
    $ctx.Credentials = $credentials  
    $web = $ctx.Web  
    $lists = $web.Lists  
    $ctx.Load($lists)  
    $ctx.ExecuteQuery()  
    $lists| select -Property Title, ID | export-csv -Path C:\lists.csv -NoTypeInformation  
    

    Reference:
    Retrieve all list names and list guids from SharePoint Online site using PowerShell

    ===========================

    To get content types name and id from a list:

    #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"  
        
    #Set parameter values  
    $SiteURL="https://crescenttech.sharepoint.com/"  
    $ListName="Projects"  
       
    #Get Credentials to connect  
    $Cred= Get-Credential  
         
    #Setup the context  
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)  
        
    #Get the List  
    $List = $Ctx.Web.lists.GetByTitle($ListName)  
       
    #Get All content types of the list  
    $ContentTypes = $List.ContentTypes  
    $Ctx.Load($ContentTypes)  
    $Ctx.ExecuteQuery()  
       
    #Get Content Type Details  
    $ContentTypes | Select Name,Description, ID  
    

    Reference:
    SharePoint Online: Get List Content Types using PowerShell
    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.