1

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
    }
}
5
  • can you do $ct.count? Commented Dec 3, 2018 at 15:19
  • Changed $item.ContentType.Id for $CT.count and it outputs 1 everytime Commented Dec 3, 2018 at 15:25
  • it seems adding ctx.load($ct) $ctx.ExecuteQuery() has got it working Commented Dec 3, 2018 at 16:27
  • @charlie - please close question or provide Your own answer with final script. Commented Dec 4, 2018 at 5:28
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented Dec 4, 2018 at 10:13

1 Answer 1

1

Instead of using

$CT = $item.ContentType

try using

$CT = $item["ContentType"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.