Creating a custom-typed folder with column values and defaults using PnP Powershell

Copper Contributor

In order to:

  • resolve a folder,
  • set its content type,
  • set values for the 4 custom columns in the content type,
  • and default column values for those 4 for the contents of the folder,

I'm making 7 different PnP cmdlet calls.

 

Which as you can imagine means a long time to do 3,000+ folders!

 

There must be a more efficient way to do this than what I've scraped together so far? 

A way to specify the content type when doing an Add- or Resolve-PnPFolder? 

Or a way to add column defaults while setting a content type? 

Or a way to set multiple column defaults at once? 

Anything?

 

$metadata = @{
    "CustomColumn1" = $column1Value; 
    "CustomColumn2" = $column2Value; 
    "CustomColumn3" = $column3Value; 
    "CustomColumn4" = $column4Value
}

# Call 1: using Get-PnPFolder instead of Resolve-PnPFolder so that we 
# know whether or not we need to do Call 3
$folder = Get-PnPFolder -Url "$libraryRelPath/$folderName" `
    -ErrorAction SilentlyContinue

If($Folder -eq $null)
{
    # Call 2: create the folder
    $folder = Add-PnPFolder -Name $folderName -Folder $libraryRelPath

    # Call 3: set the content type and column values
    Set-PnPListItem -List $library -Identity $folder.ListItemAllFields `
      -ContentType $myFolderContentType -Values $metadata
}

# Calls 4, 5, 6, and 7: set default column values on the type
Set-PnPDefaultColumnValues -List $library -Folder $folderName `
    -Field "CustomColumn1" -Value $column1Value
Set-PnPDefaultColumnValues -List $library -Folder $folderName `
    -Field "CustomColumn2" -Value $column2Value
Set-PnPDefaultColumnValues -List $library -Folder $folderName `
    -Field "CustomColumn3" -Value $column3Value
Set-PnPDefaultColumnValues -List $library -Folder $folderName `
    -Field "CustomColumn4" -Value $column4Value

 

 

0 Replies