Question

Below is my code to export rich text content to a .CSV file. The content comes from a SharePoint list. I am using CAML to fetch the content and adding it to $Content.

This code exports the data but in an unexpected format - Single value gets exported to multiple rows, the exported content should be in one row.

$Extract = "C:\Logs\KBExtract\MyExtract.csv"

$Content = "<p>This is a test RTF</p>

Hi! I’m a test file. This is some bold text, and some italic text, as well as some underline text. And a bit of  text. So we’re going to end this paragraph here and go on to a nice little list:

<p>&nbsp;</p>
<p>&bull;Item 1</p>
<p>&bull;Item 2</p>
<p>&bull;Item 3</p>
<p>&bull;Item 4</p>
<p>&nbsp;</p>
<p>And now comes a fun table:</p>
<p>&nbsp;</p>
<p>Cell 1Cell 2</p>
<p>More in cell 2Cell 3</p>
<p>Next rowNext row Next row</p>
<p>&nbsp;</p>
<p>A page break:</p>
<p>&nbsp;</p>
<p>";
New-Object -TypeName PSCustomObject -Property @{
                                RichText                =  $Content                                
                             } | Export-Csv -Path $Extract -NoTypeInformation -Append  

Exported CSV Screenshot

Is this due to commas in my content? I have around 50,000 rich text values to be exported into 50,000 rows in .CSV file. But content gets mixed within the rows. What is the best way to handle this?

Was it helpful?

Solution

Run a replace or regex over the text and remove any line feeds or carriage returns. I would do it like this:

$chars = '`n', "$([char]10)", "$([char]13)"
$pattern = [string]::join('|', ($chars | % {[regex]::escape($_)}))
$foo = $item["LongtextField"] -replace $pattern

Char[10] is a new line, line feed Char[13] is a carriage return For some reason I have found I also need the newline represented by `n but you can test for yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top