How to export managed metadata from subfolders and documents to csv

Glenn Skinner 1 Reputation point
2024-09-19T15:21:09.7533333+00:00

I have a document library and at the top level we have a number of folders for each year i.e. 2024, 2023, 2022, 2021 etc. Against these yearly folders we have a Content Type of "Yearly Folder".

Under each yearly folder we have a huge list of Folders each folder is classed as a case and has a Content type of "Case Folder"

In the script if I change the value in this line <Query><Where><Eq><FieldRef Name='ContentType'/><Value Type='Text'>Case Folder</Value></Eq></Where></Query></View>"

to Yearly Folder then I get the information of the top level yearly folders but when i change it to Case Folder. The script runs but nothing is exported

I currently am using the below script but it doesn't seem to be working.

#Add-Content -Path C:\ManagedMetadata.csv -Value '"Case Folder", "CaseName"'

#Get the following folders

#Case Number / Folder Name CaseNumber

#Case Name

#Case Type

#Aircraft Type

#Case GUID

#########################################

#Set following three variables before run

$siteUrl ="https://url here"

#$siteUrl ="https://url here/"

$libraryName = "Case"

$csvLocation = "C:\ManagedMetadata.csv"

#########################################

#Add folder header

$folderHeader = "Location,Title,Case Number,Case Name,Case Type,Aircraft Type,Case GUID,"

Add-Content -Path $csvLocation -Value $folderHeader

Connect-PnPOnline -url $siteUrl -UseWebLogin

$items = Get-PnPListItem -List $libraryName -Query "<View><ViewFields><FieldRef Name='Title' /><FieldRef Name='CaseNumber' /><FieldRef Name='CaseName' /><FieldRef Name='CaseType' /><FieldRef Name='AircraftType' /><FieldRef Name='CaseGUID' /></ViewFields><Query><Where><Eq><FieldRef Name='ContentType'/><Value Type='Text'>Case Folder</Value></Eq></Where></Query></View>"

foreach($item in $items){

$folderInfo += '","'

$folderInfo += $item.FieldValues["Title"]

$folderInfo += '","'

$folderInfo += $item.FieldValues["CaseNumber"]

$folderInfo += '","'

$folderInfo += $item.FieldValues["CaseName"]

$folderInfo += '","'

$folderInfo += $item.FieldValues["CaseType"].Label

$folderInfo += '","'

$folderInfo += $item.FieldValues["AircraftType"]

$folderInfo += '","'

$folderInfo += $item.FieldValues["CaseGUID"]

$folderInfo += '",'

$folderInfo

Add-Content -Path $csvLocation -Value $folderInfo

}

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,598 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,948 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,468 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 46,561 Reputation points
    2024-09-19T15:55:38.8333333+00:00

    I don't know if this helps at all, but since you have a "PowerShell" tag on this question, here's a less cluttered version of your script:

    #Add-Content -Path C:\ManagedMetadata.csv -Value '"Case Folder", "CaseName"'
    #Get the following folders
    #Case Number / Folder Name CaseNumber
    #Case Name
    #Case Type
    #Aircraft Type
    #Case GUID
    
    #########################################
    #Set following three variables before run
    $siteUrl = "https://url here"
    $libraryName = "Case"
    $csvLocation = "C:\ManagedMetadata.csv"
    #########################################
    
    $row = [ordered]@{}
    Connect-PnPOnline -url $siteUrl -UseWebLogin
    $Query = "<View><ViewFields><FieldRef Name='Title' /><FieldRef Name='CaseNumber' /><FieldRef Name='CaseName' /><FieldRef Name='CaseType' /><FieldRef Name='AircraftType' /><FieldRef Name='CaseGUID' /></ViewFields><Query><Where><Eq><FieldRef Name='ContentType'/><Value Type='Text'>Case Folder</Value></Eq></Where></Query></View>"
    Get-PnPListItem -List $libraryName -Query $Query |
        ForEach-Object{
            $row.Clear                                          # remove any previous keys and values
            $row["Location"]        = "????"                    # $LibraryName maybe???
            $row["Title"]           = $_.FieldValues["Title"]
            $row["CaseNumber"]      = $_.FieldValues["CaseNumber"]
            $row["CaseName"]        = $_.FieldValues["CaseName"]
            $row["CaseType"]        = $_.FieldValues["CaseType"].Label
            $row["AircraftType"]    = $_.FieldValues["AircraftType"]
            $row["CaseGUID"]        = $_.FieldValues["CaseGUID"]
            [PSCustomObject]$row
    } | Export-CSV $csvLocation -NoTypeInformation
    

    I don't know what goes into the "Location" column of your CSV since that line appears to be missing in your posted code.One of the SharePoint folks (I hope) will help with the query problem.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.