1

I am unable to get the specific list/library contenttypes using csom powershell.

Getting error:"The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested."

Below is code I used to get content type collection:

    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
    $Context.Credentials = $credentials
    $Context.ExecuteQuery()

    $Web= $Context.Web
    $Context.Load($Web)
    $Context.ExecuteQuery()


    $listsColl=$Web.Lists
    $Context.Load($listsColl)
    $Context.ExecuteQuery()

    $contentTypeColl=$Context.Site.RootWeb.ContentTypes
    $Context.Load($contentTypeColl)
    $Context.ExecuteQuery()

    $CT=$contentTypeColl | where {$_.Id -eq "0x010100793AA7D56406304698C72BB5734F0C25"}
    $Context.Load($CT)
    $Context.ExecuteQuery()     

    $list=$listsColl.GetByTitle("1_Uploaded Files")
    $Context.Load($list)
    $Context.ExecuteQuery()
0

Sample script to get list Content Types using SharePoint Online CSOM for your reference:

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://xxx.sharepoint.com/sites/xxx/"

$UserName="xxx@xxx.onmicrosoft.com"
$Password ="xxx"

#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $credentials
$Context.ExecuteQuery()
$listsColl=$Context.Web.Lists
$Context.Load($listsColl)
$Context.ExecuteQuery()
$list=$listsColl.GetByTitle("testdoc1")
$Context.Load($list)
$Context.ExecuteQuery()
$Contenttypes = $list.ContentTypes
$context.Load($Contenttypes)
try
{
    $context.ExecuteQuery()
    Write-Host "Successfully retrived list content types" -foregroundcolor black -backgroundcolor green
}
catch
{
    Write-Host "Error while getting the $ListName List content types $_.Exception.Message" -foregroundcolor black -backgroundcolor Red  
}

Write-Host "Displaying $ListName List content type started....." -foregroundcolor black -backgroundcolor yellow  
foreach($contentType in $Contenttypes)
{
    Write-Host $contentType.Name
}
Write-Host "Displaying List Content types Completed" -foregroundcolor black -backgroundcolor Green 

enter image description here

Reference:

https://www.sharepoint-journey.com/get-list-content-types-in-ofice-365-using-powershell-and-csom.html

0

So the point is.
This DOES NOT WORK

$contentTypeColl | where {$_.Id -eq "0x010100793AA7D56406304698C72BB5734F0C25"}



This WORKS

$contentTypeColl | foreach { $_.Id } | where { $_ -eq "0x010100793AA7D56406304698C72BB5734F0C25"}


But I don't know why.

  • not a problem with this $contentTypeColl | where {$_.Id -eq "0x010100793AA7D56406304698C72BB5734F0C25"} or the second one.... problem was after loading list content types ,i called execute query..then attached contenttype..when this execute query got executed my contenttype collection got empty....so in the above posted code by Jerry_MSFT i noticed and removed execute query method after loading contenttype collection – Swetha Paritala Sep 21 '18 at 12:44

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.