0

I am using the script below to export a 6000+ item Sharepoint list to CSV. It works great, however, 1 of the columns on the list is a Managed Metadata column. When exported with the script below, the values are all 'Microsoft.SharePoint.Client.Taxonomy.TaxonomyFieldValue'.

I have tried a few variations but I am struggling. Please could anyone help me to change the script below to work with the Managed Metadata column?

Function GetAllListItems($ctx,$listUrl)
{
   #Get the List
    $List = $Ctx.Web.GetList($ctx.Web.ServerRelativeUrl + "/" + $listUrl)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
 
    #Define Query to get List Items in batch
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = @"
    <View Scope='RecursiveAll'>
        <Query>
            <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
        </Query>
        <RowLimit Paged="TRUE">5000</RowLimit>
    </View>
"@
 

    $allListItems = @()
    #Get List Items in Batch
    Do
    {
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()
        #$ListItems.count
        $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition

       foreach($listItem in $ListItems)
       {
            $allListItems += $listItem
       }


    }
    While($Query.ListItemCollectionPosition -ne $null)

    Return $allListItems


}


Function MainProcress()
{

    $siteUrl = "SITE URL HERE"
    $listUrl = "Lists/List Name Goes Here"
    $outputFilePath = "F:\exportedlist.csv"

    $includeHiddenField = $False
    $includeReadonlyField = $False

    Connect-PnPOnline -Url $siteUrl -Interactive

    $ctx = Get-PnPContext
    $ctx.Load($ctx.Web)
    $ctx.ExecuteQuery()

    $listFields = Get-PnPField -List $listUrl |? { $_.Hidden -eq $includeHiddenField -AND $_.ReadOnlyField -eq $includeReadonlyField} | Select -ExpandProperty internalname

    $count = 0

    $listItems= GetAllListItems -ctx $ctx -listUrl $listUrl

    $listItems.Count
    $hashTable=@()  

    # Loop through the list items  
    foreach($listItem in $listItems)  
    {   $count = $count+1
        Write-Progress -Activity "Exporting" -Status "$($count/$listItems.Count*100)% Complete:" -PercentComplete $($count/$listItems.Count*100)
        $obj=New-Object PSObject              

        $listItem.FieldValues.GetEnumerator() | Where-Object { $_.Key -in $listFields }| ForEach-Object{ 
                                        
                    if($_.Value.LookupValue){$obj | Add-Member Noteproperty $_.Key $_.Value.LookupValue}
                    else{$obj | Add-Member Noteproperty $_.Key $_.Value}                                        
                    
                   
                }
        
        $hashTable+=$obj;  
        $obj=$null;  
    }  
     $hashtable | export-csv $outputFilePath -NoTypeInformation  

}

MainProcress

1 Answer 1

0

I'd create a new object for each list item in the loop and fill it with data I need.

Your Answer

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

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