Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add additional UseDisplayName flag to metadata extraction script #515

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions examples/Metadata Extraction/Metadata-extraction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ param (
[string]$FolderID = "",

# Set to a specific Box user id if you would like to pull metadata as a specific user instead of the current user
[string]$UserId = ""
[string]$UserId = "",

# If enabled, the "displayName" of the metadata template field will be used as the header instead of the "key"
[switch]$UseDisplayName = $false
)

#############################################################################
Expand Down Expand Up @@ -229,6 +232,7 @@ Function Start-Metadata-Extraction {

Write-Log "Output $($Entries | Out-String)"
$MetadataTemplatesHashmap = @{}
$HeaderFieldName = If ($UseDisplayName) { 'displayName' } Else { 'key' }

ForEach ($Item in $Entries) {
$ItemID = $Item.id
Expand Down Expand Up @@ -267,7 +271,6 @@ Function Start-Metadata-Extraction {
$TemplateKey = $MetadataValue."`$template"

#Pull MetadataTemplate to get access to all it's fields and put it in a hashmap.
#Next, to the first object being added, we include all missing fields from the metadata template to ensure the corresponding columns are added to the CSV file.
if (!$MetadataTemplatesHashmap.ContainsKey($TemplateKey)) {
Write-Log "Pulling MetadataTemplate for templateKey: $TemplateKey" -output true -color Green
Try {
Expand All @@ -279,23 +282,25 @@ Function Start-Metadata-Extraction {

$MetadataTemplate = $MetadataTemplateResp | ConvertFrom-Json
$MetadataTemplatesHashmap[$TemplateKey] = $MetadataTemplate

foreach ($MetadataTemplateField in $MetadataTemplatesHashmap[$TemplateKey].fields){
#To maintain the continuity of fields from the metadata template in the resulting CSV file, we remove and then add them at the end
if ($MetadataValue.PSObject.Properties.Name -contains $($MetadataTemplateField.key)) {
$metadataFieldValue = $MetadataValue.$($MetadataTemplateField.key);
$MetadataValue.PSObject.Properties.Remove($($MetadataTemplateField.key));
$MetadataValue | Add-Member -NotePropertyName "$($MetadataTemplateField.key)" -NotePropertyValue $metadataFieldValue;
} else {
#We add a field with an empty value so that it's included in the CSV file header, ensuring that subsequent items with this field will also be added
$MetadataValue | Add-Member -NotePropertyName "$($MetadataTemplateField.key)" -NotePropertyValue $null;
}
}
} Catch {
Write-Log "Could not get the metadata template for item. See error log for details." -errorMessage $MetadataTemplateResp -output True -color Red
}
}

#Prepare the MetadataValue object by setting its fields name based on the passed UseDisplayName flag,
#which determines the name of the field used in the variable HeaderFieldName.
foreach ($MetadataTemplateField in $MetadataTemplatesHashmap[$TemplateKey].fields){
#To maintain the continuity of fields from the metadata template in the resulting CSV file, we remove and then add them at the end
if ($MetadataValue.PSObject.Properties.Name -contains $($MetadataTemplateField.key)) {
$metadataFieldValue = $MetadataValue.$($MetadataTemplateField.key);
$MetadataValue.PSObject.Properties.Remove($($MetadataTemplateField.key));
$MetadataValue | Add-Member -NotePropertyName "$($MetadataTemplateField.$HeaderFieldName)" -NotePropertyValue $metadataFieldValue;
} else {
#We add a field with an empty value so that it's included in the CSV file header, ensuring that subsequent items with this field will also be added
$MetadataValue | Add-Member -NotePropertyName "$($MetadataTemplateField.$HeaderFieldName)" -NotePropertyValue $null;
}
}

#Append Object Info values: Name, Object Id, Type
$MetadataValue | Add-Member -NotePropertyName "Name" -NotePropertyValue $Item.name;
$MetadataValue | Add-Member -NotePropertyName "Object Id" -NotePropertyValue $Item.id;
Expand Down
2 changes: 2 additions & 0 deletions examples/Metadata Extraction/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Extracts metadata details for all the files and folders in any Box folder and sa
## 1. Script Parameters
1. Set the [folderID][folderID-param] you want the script to scan for metadata details.
2. Optional: To run the script as another user, set [userId][UserId-param] parameter.
3. Optional: Use the [-UseDisplayName][UseDisplayName-param] switch when running the script to ensure that the headers of the resulting file correspond to the `displayName` field instead of the `key` in the metadata template.


## 2. Run the script
Expand Down Expand Up @@ -76,3 +77,4 @@ This project is a collection of open source examples and should not be treated a
[oauth-guide]: https://developer.box.com/guides/cli/quick-start/
[FolderID-param]: /examples/Metadata%20Extraction/Metadata-extraction.ps1#L11
[UserID-param]: /examples/Metadata%20Extraction/Metadata-extraction.ps1#L14
[UseDisplayName-param]: /examples/Metadata%20Extraction/Metadata-extraction.ps1#L17
Loading