0

I have been able to create an array and store a list of fields, I am trying to convert this to a CSV file. However, I need all my outputs to be as a string. Currently I have text coming out as a string, however for example I need my note to come out as a string but it comes out as taxonomy value and not sure how to change this?

Below is my code I have just now:

# Get the web
$web = Get-SPWeb "specified_website"

# Get the target list
$list = $web.Lists["List_name"]

# Items used to hold the data
$Items = $list.Items

$array = @()

# Get all the items in the list
foreach ($item in $Items) {
    $id = $item.id
    Write-Host "Processing item number $id" -ForegroundColor Green
    # every item has a Field e.g. my item has a title, etc.
    foreach ($field in $item.Fields) {
        # Get the type of the field
        Write-Host $field.Type

        # Print the field type out to a string
        switch ($field.Type) {
            "Text" { $msg = checkifTextisNull($item[$field.InternalName]) }
            "Note" {
                Write-Host $item[$field.InternalName] -ForegroundColor Cyan
            }
            "Lookup" { $item[$field.InternalName] -ForegroundColor Yellow }
            "URL" { Write-Host $item[$field.InternalName] -ForegroundColor Magenta }
            "Invalid" {
                #Write-Host $item[$field.InternalName] -ForegroundColor Cyan
            }
            "Guid" { Write-Host $item[$field.InternalName]-ForegroundColor Yellow }
            "Choice" { Write-Host $item[$field.InternalName] -ForegroundColor Cyan }
            "Boolean" {
                $msg = returnBooleanValue($item[$field.InternalName])
                Write-Host $msg -ForegroundColor DarkCyan
            }
            "Computed" { Write-Host $item[$field.InternalName] -ForegroundColor White }
            "Integer" {
                Write-Host $item[$field.InternalName]
            }
            "User" { Write-Host $item[$field.InternalName] -ForegroundColor DarkGreen }
            "DateTime" {
                Write-Host $item[$field.InternalName] -ForegroundColor DarkGreen
            }
            "Number" { Write-Host $item[$field.InternalName] -ForegroundColor Yellow }
            "Counter" { Write-Host $item[$field.InternalName] -ForegroundColor Green }
            "Multi-Choice" {
                Write-Host $item[$field.InternalName]
            }
            "Attachments" { Write-Host $item[$field.InternalName] -ForegroundColor Magenta }
            "ModStat" { Write-Host $item[$field.InternalName] -ForegroundColor DarkGreen }
            "File" { Write-Host $item[$field.InternalName] -ForegroundColor White }
        }
    }

    # Add the object with property to Items
    $array += $Items
}

My output that I get is:

Note

Firm wide|0d3fbace-af9c-4f28-8be1-095e616893c0

And the Output I would expect is:

"FieldName", "DataType", "Value"

Site_type_0, Note , Firm Wide

When I Write out what the type of data is, I get taxonomy data, when I would want a string

  • 5
    Exporting to CSV should automatically convert your data to text. Please provide an example of desired and actual output. – Ansgar Wiechers Dec 01 '17 at 15:43
  • ListItems ParentList Microsoft.SharePoint.SPListItemCollection Site Information Microsoft.SharePoint.SPListItemCollection Site Information – Ryan Archibald Dec 01 '17 at 15:59
  • this is what I get from the CSV file, surely the "Microsoft.SharePoint.SPListItemCollection" should be a string? – Ryan Archibald Dec 01 '17 at 16:00
  • I also get a type of Taxonomy which I'm not sure what that is for Note nor how to convert that to string – Ryan Archibald Dec 01 '17 at 16:00
  • 3
    Please *show* the actual content of a sample output file you created (CSVs are text files, so open the file in an editor and copy/paste the text). Also show a second sample of how that output is supposed to look like. [Edit] your question to do so. Do not bury this information in comments. – Ansgar Wiechers Dec 01 '17 at 16:12
  • Try [Flatten-Object](https://stackoverflow.com/a/46081131/1701026): `$Web | Flatten` – iRon Dec 01 '17 at 17:32
  • I have tried Flatten-Object on the $Web but still getting the same output. – Ryan Archibald Dec 04 '17 at 09:19

1 Answers1

1

I found that using .TypeAsString helped me out a lot and for the TaxonomyFieldType and TaxonomyFieldTypeMulti Field types I done the following:

"TaxonomyFieldType"{ 
                $FieldValue = $item[$field.InternalName] -as [Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue];                    
                $FieldValue = $FieldValue.Label;
                 $FieldValue
        }
        "TaxonomyFieldTypeMulti"{$FieldValueCollection = $item[$field.InternalName] -as [Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection];    

                foreach($Value in $FieldValueCollection)   {

                    if($Value.label -ne $null)
                            {
                            $label = $Value.label
                            $guid = $Value.TermGuid.ToString()
                            $FieldValue+="Label = $label Guid = $guid;"
                            }   
                }     
                Write-Host $Value.label
        }

If there is anyone who knows a better way please let me know as I would be very interested to hear.