I have the following script which should output the name of the content type for each list item however it is not, where am I going wrong? The output for both Id and Name is just a blank line, clearly loops through each item yet doesn't find the content type field

$qCommand = @"
<View Scope="RecursiveAll">
    <Query>
        <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
    </Query>
    <RowLimit Paged="TRUE">5000</RowLimit>
</View>
"@
## Page Position
$position = $null

## All Items
$allItems = @()
Do
{
    $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
    $camlQuery.ListItemCollectionPosition = $position
    $camlQuery.ViewXml = $qCommand

    ## Executing the query
    $currentCollection = $list.GetItems($camlQuery)
    $ctx.Load($currentCollection)
    $ctx.ExecuteQuery()

    ## Getting the position of the previous page
    $position = $currentCollection.ListItemCollectionPosition

    # Adding current collection to the allItems collection
    $allItems += $currentCollection
}
# the position of the last page will be Null
Until($position -eq $null) 

$results = ""
foreach($item in $allItems)
{ 
    $CT = $item.ContentType
    write-host $item.ContentType.Id
    write-host $item.ContentType.Name
    if($CT -eq "Land")
    {
        $item["FileLeafRef"] | Out-File "C:\temp\test.csv" -append
    }
}
  • can you do $ct.count? – Waqas Sarwar MVP Dec 3 at 15:19
  • Changed $item.ContentType.Id for $CT.count and it outputs 1 everytime – charlie Dec 3 at 15:25
  • it seems adding ctx.load($ct) $ctx.ExecuteQuery() has got it working – charlie Dec 3 at 16:27
  • @charlie - please close question or provide Your own answer with final script. – Kristaps Vilerts Dec 4 at 5:28
  • Comments are not for extended discussion; this conversation has been moved to chat. – Mohamed El-Qassas MVP Dec 4 at 10:13

Instead of using

$CT = $item.ContentType

try using

$CT = $item["ContentType"]

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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