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 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,958 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,489 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 46,636 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

  2. Ling Zhou_MSFT 17,135 Reputation points Microsoft Vendor
    2024-09-20T06:17:09.71+00:00

    Hi @Glenn Skinner,

    Thank you for posting in this community.

    Try adding the -FolderServerRelativeUrl parameter to specify that the results are fetched from a certain folder.

    Get-PnPListItem -List Samples -FolderServerRelativeUrl "/sites/Spe24/Document1/2024%20Yearly%20Folder"
    

    User's image

    Reference: Get-PnPListItem.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Ling Zhou_MSFT 17,135 Reputation points Microsoft Vendor
    2024-09-25T05:29:06.93+00:00

    Hi @Glenn Skinner,

    Sorry for the late reply, as it took me a little time to find a solution.

    This issue occurs because SharePoint Online uses the Large List Resource Throttling feature. By default, the list view threshold is configured at 5,000 items. When the defined item limit in a list is exceeded, the message is displayed.You can add <RowLimit>1000</RowLimit> in the View tag in your query. This way we can fix the error.

    Reference: SharePoint Online: Get List Items from Large Lists ( >5000 Items) using PowerShell without List View Threshold Exceeded Error.

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link. 

    For Question: Also this would only cover the 2024 yearly folder but i have folders for every year going back to 2012 so would I need to run it separately for each year

    I'm afraid yes, we need to set the name of Yearly Folder manually, because in -FolderServerRelativeUrl we are specifying to get the contents of the Yearly Folder.

    In addition, exporting by year is not prone to 5000 view threshold errors.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.