SharePoint Online: PowerShell to Get Content Type

Requirement: Get Content Types in SharePoint Online using PowerShell

PowerShell to Get Content Type by Content Type ID:
#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"

#parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ContentTypeID="0x01002A7A908ACAB0054880702EE263AC762B"
 
#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 content type by ID
$ContentType = $Ctx.web.ContentTypes.GetById($ContentTypeID)
$Ctx.Load($ContentType)
$Ctx.ExecuteQuery()
 
If($ContentType -ne $Null)
{
    #Get Columns from the content type
    $Ctx.Load($ContentType.Fields)
    $Ctx.ExecuteQuery()

    #Get columns from the content type
    ForEach($Field in $ContentType.Fields)
    {           
        Write-Host -f Green $Field.Title
    } 
}
else
{
    Write-host "Content Type '$ContentTypeName' doesn't exist!'" -f Yellow
}

SharePoint Online: PowerShell to Get Content Type by Name
Here is the how to get content type by name in SharePoint Online using 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 Get-SPOContentTypes()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName
    )
 
    Try {
        #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 content types from web
        $ContentTypes = $Ctx.Web.ContentTypes
        $Ctx.Load($ContentTypes)
        $Ctx.ExecuteQuery()
 
        #sharepoint online powershell get content type by name
        $CType = $ContentTypes | Where {$_.Name -eq $ContentTypeName}
        If($CType -ne $Null)
        {
            #Get Columns from the content type
            $Ctx.Load($CType.Fields)
            $Ctx.ExecuteQuery()
 
            $ResultArray = @()
            #Loop through the Fields in the Content Type
            ForEach ($Field in $CType.Fields)
            {
                #Create a new custom object to hold our row of data with property names:
                $Result = New-Object PSObject
                $Result | Add-Member -MemberType NoteProperty -Name "Title" -Value $Field.Title
                $Result | Add-Member -MemberType NoteProperty -Name "Internal Name" -Value $Field.InternalName
                $Result | Add-Member -MemberType NoteProperty -Name "ID" -Value $Field.Id
                $Result | Add-Member -MemberType NoteProperty -Name "Group" -Value $Field.Group
                $Result | Add-Member -MemberType NoteProperty -Name "Description" -Value $Field.Description
 
                #Add the object to array
                $ResultArray += $Result
            }
            $ResultArray | Format-table -AutoSize
        }
        else
        {
            Write-host "Content Type '$ContentTypeName' doesn't exist!'" -f Yellow
        }
   }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#Set parameter values
$SiteURL ="https://crescent.sharepoint.com/sites/marketing"
$ContentTypeName="Announcement"

#Call the function
Get-SPOContentTypes -SiteURL $SiteURL -ContentTypeName $ContentTypeName

SharePoint Online: PowerShell to List Content Types
Let's get all content types from a SharePoint Online site using 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"
 
#Config Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
 
Try {
    #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 all content types from the web
    $ContentTypes = $Ctx.Web.ContentTypes
    $Ctx.Load($ContentTypes)
    $Ctx.ExecuteQuery()
  
    #List Content Type Name, Description, ID and Group
    $ContentTypes | Select Name, Description, Id, Group | Format-List  
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}
This PowerShell lists content types from SharePoint Online site. Here is my another post to get all content types from list: SharePoint Online: PowerShell to Get List Content Types

To export all content types to CSV or HTML report using PowerShell, refer: generate a report: SharePoint Online: Get Content Types Report using PowerShell 
SharePoint Online: PowerShell to Get Content Type SharePoint Online: PowerShell to Get Content Type Reviewed by Salaudeen Rajack on July 13, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.