powershell icon indicating copy to clipboard operation
powershell copied to clipboard

[BUG] Set-PnpDefaultColumnValues fails with folders and Managed Metatdata, potentially corrupts library

Open denicomp opened this issue 1 year ago • 6 comments

Reporting an Issue or Missing Feature

When creating a Managed Metadata term, if a parent term has a custom property set (using -CustomProperties) and there are multiple levels of folders in the Document Library, Set-PnpDefaultColumnValues at the lowest folder level will fail after 3 times. It then creates a Default Column Value in the Document Library which says "Current value invalid" and it cannot be removed and you cannot use Set-PnpDefaultColumnValues on the library any longer - it will fail with an "Index was outside the bounds of the array" on anything in the library - the library has to be deleted and recreated.

Expected behavior

Set-PnpDefaultColumnValues should not fail after 3 times. And it does not fail if you do not use Custom Properties on the parent term.

Actual behavior

I have a Document Library with two Managed Metadata fields ("Bank" and "Branch"). Both point to the same Term in a Term Set.

I have a script that creates a term using New-PnPTerm with the -CustomProperties option. Then it creates a folder in the root of the Document Library and sets a default column value using Set-PnpDefaultColumnValues on the "Bank" field with the value of the term just created.

Then within that new folder, it creates another folder ("Container") with no default values. (This is required - it does not happen if it doesn't create this.)

Then within that new "Container" folder, the script creates a new term under the term created above, creates a folder in the "Container" folder, and sets the default column value on the "Branch" field to the term just created.

It repeats the above step in a loop, and it will fail on Set-PnPDefaultColumnValues with the error "Index was outside the bounds of the array".

After that, any call to Set-PnPDefaultColumnValues on any folder at any level will fail with that error.

When looking at the Library Settings, "Change default column values", it will show the following on the second folder that it created: Screenshot 2022-07-13 215839

Steps to reproduce behavior

The problem can be reproduced with these steps and this script.

  1. In the Term Store, create a Term Group called "Banks"
  2. Under the Term Group "Banks", create a Term Set called "Branches" Screenshot 2022-07-13 220220
  3. In a site, create a Document Library called "Bank Library" (or whatever)
  4. In that Doc Lib, add a Managed Metadata column named "Bank" and select "Branches" from the Term Store
  5. In that Doc Lib, add a Managed Metadata column named "Branch" and select "Branches" from the Term Store

Run the following script, editing the first lines to point to the site and the name of the Doc Lib created above.

What this script will do is:

  • Creates a Term under Banks with a custom property (the custom property is required or it will not fail)
  • Creates a folder in Bank Library and sets the Default Value for the Bank field to the Term created above
  • Creates a folder inside the folder just created named "Container" (this is required or it will not fail)
  • In a loop
      • Creates a Term with the parent of the Term created above using Add-PnpTermToTerm
      • Creates a folder inside "Container" and sets the Default Value for the Branch field to the Term just created
  • On the 3rd time through the loop, setting the Default Value for the Branch field will fail on Set-PnPDefaultColumnValues
  • The Default Column value on the second folder created will show as "Invalid" and Set-PnPDefaultColumnValues will fail on anything in that Document Library and the Default Value cannot be removed from the UI - it will fail with an exception.
$ErrorActionPreference = "Stop"

$site = "https://xyz.sharepoint.com/sites/TestSite"
$libraryName = "Bank Library"
$libraryPath = "/sites/TestSite/$libraryName"

Connect-PnpOnline -Url $site -Interactive

# Add Bank term
Write-Output "Create Bank Term"
$bankID = (Get-Random -Minimum 10000 -Maximum 99999).ToString()
$bankName = "Bank $bankID"
# Create a new Term for Bank with a custom property - this is required to illustrate the problem
New-PnPTerm -TermSet "Branches" -TermGroup "Banks" -Name $bankName -CustomProperties @{"BankID"=$bankID} | Out-Null

### ----- NOTE ----- If the -CustomProperties option above is removed, this will run without errors

# Create Bank folder
Write-Output "Create Bank Folder"
Add-PnPFolder -Name $bankName -Folder $libraryName | Out-Null

# Set the default value for it
Set-PnPDefaultColumnValues -List $libraryName -Field "Bank" -Value "Banks|Branches|$bankName" -Folder "/$bankName" | Out-Null

# Create a container folder under Bank - this is required to illustrate the problem
Write-Output "Create Container Folder"
Add-PnpFolder -Name "Container" -Folder "$libraryPath/$bankName" | Out-Null

for($i = 0; $i -lt 4; $i++)
{
	# Create a Branch term value
	$branchID = (Get-Random -Minimum 1000000 -Maximum 9999999).ToString()
	$branchName = "Branch $branchID"

	# Get the current terms under the Branches because we need the ID of the parent
	$bankTerms = Get-PnpTerm -TermSet "Branches" -TermGroup "Banks" -Identity $bankName -IncludeChildTerms -Recursive

	Write-Output "Add Branch to Branches: $bankName -> $branchName"
	Add-PnpTermToTerm -ParentTerm $bankTerms.Id -Name $branchName | Out-Null

	Write-Output "Create Branch Folder: $branchName"
	Add-PnpFolder -Name $branchName -Folder "$libraryName/$bankName/Container" | Out-Null

	# Set the default value for Branch on it (this will fail on the 3rd time through)
	Set-PnPDefaultColumnValues -List $libraryName -Field "Branch" -Value "Banks|Branches|$bankName|$branchName" -Folder "/$bankName/Container/$branchName" | Out-Null
}

Screenshot 2022-07-13 221711

What is the version of the Cmdlet module you are running?

1.11.11

Which operating system/environment are you running PnP PowerShell on?

  • [X ] Windows
  • [ ] Linux
  • [ ] MacOS
  • [ ] Azure Cloud Shell
  • [ ] Azure Functions
  • [ ] Other : please specify

denicomp avatar Jul 14 '22 02:07 denicomp

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] avatar Dec 24 '22 02:12 github-actions[bot]

@denicomp - Can you please update to the latest release 2.1.x however you will need PS 7.2 or later to benefit from all the bug fixes and improvements.

PS5 support has been dropped in the new release.

Feel free to reopen if the issue persists. Thanks!

veronicageek avatar Apr 11 '23 12:04 veronicageek

I'm still getting this, on PnP.PowerShell v 2.2.0 and PowerShell 7.3.x

HiltonGiesenow avatar Nov 01 '23 08:11 HiltonGiesenow

Same error for me... PnP.PowerShell v 2.2.0 Powershell v 7.3.8

h-marti avatar Nov 03 '23 09:11 h-marti

In my experience this "Index was outside the bounds" response if there are any entries in the client_LocationBasedDefaults.html file which have completely blank values.e.g. <DefaultValue FieldName="Year"></DefaultValue>

This in turn can happen quite easily just by calling Set-PnPDefaultColumnValues on a managed metadata column where it is unable to resolve the value properly. In my case because of misaligning the term store hierarchy by specifying:

-Value FilestoreTraining|Year|1920

where it should have been:

-Value FilestoreTraining|FilestoreTraining|Year|1920

Without going over the script/scenario in the original post in massive detail I believe that the second run around the loop is not able to resolve the value which is to become the default so it adds and a blank entry and this means for subsequent loops it will get the "Index was outside...".

Workaround As others have pointed out this is also a situation that the GUI is completely unable to handle or recover from. I am having to edit the client_LocationBasedDefaults.html file directly to fix the empty entry, or as is proving easier in most cases just reset the file completely - I've set up a PowerAutomate Flow that does this by asserting the content to become: <MetadataDefaults> </MetadataDefaults>

Summary This issue seems to be rooted in ending up with a corrupted Location-based defaults value settings file and I believe Set-PnPDefaultColumnValues should be erroring in cases where it cannot resolve a managed metadata value rather than entering a blank one.

pinkerda avatar Feb 02 '24 12:02 pinkerda

Was receiving the message "Index was outside the bounds of the array". Upgrading to PnP.PowerShell 2.4.0 solved the issue for me.

ssamnadda avatar May 06 '24 21:05 ssamnadda