0

I am trying to get a list of content types with PowerShell using CSOM. I was able to get list of fields, however when I try to get a list of content types, it's empty.. here's the code am using:

$password = "somepassword" | ConvertTo-SecureString -AsPlainText -Force
$context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("email",$password)

$fields = $context.web.fields
$contentTypes = $context.web.ContentTypes
$context.load($fields)
$context.load($contentTypes)

$context.ExecuteQuery()

when I check the list of fields, I can see results there, however content types is always empty. Any reason?

0

The following script worked for me.

cls
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\PowerShell\Microsoft.SharePoint.Client.Runtime.dll"


#Config Parameters
$SiteURL="https://tenant.sharepoint.com/sites/TestSite"

 #Get credentials to connect
 $Cred= Get-Credential
 

Try {
    #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 Web
    $Web = $Ctx.Web
    $Ctx.Load($Web)

 
    #Get All Site Columns of the Web
    $SiteColumns = $Web.Fields
    $Ctx.Load($SiteColumns)
    $Ctx.ExecuteQuery() 
 
     #Get the Site Columns and Group
    Write-Host "Available Site Columns"
    Write-Host "-------------------------------------------------"
    #Get the Site Column Name and Group
    $SiteColumns | Select Title, Group 


    #Get All Site Columns of the Web
    $SiteContentTypes = $Web.ContentTypes
    $Ctx.Load($SiteContentTypes)
    $Ctx.ExecuteQuery() 

    #Get the Site Content Type and Group
    Write-Host "Available ContentTypes"
    Write-Host "-------------------------------------------------"
    $SiteContentTypes | Select Title, Group 

    Write-Host "Number of Colunms: $($SiteColumns.Count)"
    Write-Host "Number of Content Types: $($SiteContentTypes.Count)"

}
Catch {
    write-host -f Red "Error: " $_.Exception.Message
}

Reference: check out here

0

I could not reproduce your issue:

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"  

$SiteUrl = "https://contoso.sharepoint.com/sites/dev"

$UserName="amos@contoso.onmicrosoft.com"
$Password ="password"
  
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
Try {

    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $credentials
    $fields = $context.web.fields
    $contentTypes = $context.web.ContentTypes
    $context.load($fields)
    $context.load($contentTypes)
    
    $context.ExecuteQuery()

    Write-host $fields
    Write-host $contentTypes
    
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.