how to use powershells sharepoint PnP module change columns conditional formulas?

Copper Contributor

In sharepoinm, I have a custom formula for a column , called column A, that looks like  this:

 

=if[$myColumnB],'true','false')

I need to do this 200 times, with different columns, how do I use powershell to achieve this? Or where do I ask this question.

 

-Thank you

7 Replies

@childishbambino You can use PnP PowerShell code like below to apply conditional formula to list fields: 

 

$conditionalFormula = "=if[{0}],'true','false')" -f '$myColumnB'
 
# Get SP list field
$field = Get-PnPField -List "Tasks" -Identity "ColumnA"
 
# Apply conditional formula to column
$field.ClientValidationFormula = $conditionalFormula
$field.Update()
Invoke-PnPQuery

 

Where ColumnA is the internal name of your SharePoint column. You can get the internal name of column using various methods shown in this article: How to find the Internal name of columns in SharePoint Online? 


Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

does columnB need to be internal or external?

@dperez13 ColumnB is the internal name of your SharePoint list column from same SharePoint list where you are updating the column and conditional formula.


Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

disregard, okay, now how do I acesss the sharepoint list that I want to make these chages on?

Import-Module -Name PnP.PowerShell -Debug -Force

Connect-PnPOnline -Url

$site =

$sharedDocumentsURL = $site+"/Shared Documents"

$sharedFiles = Get-PnPListItem -List "Shared Documents"

foreach ($file in $sharedFiles) {

Write-Host "File name: $($file["FileLeafRef"])"

}

I have this, but i do not care about the shared documents, I need to access the SPO list so I can use your code on it, how doI do this?

@dperez13 Use this code: 

 

$siteUrl = "https://contoso.sharepoint.com/sites/siteName"
Connect-PnPOnline -Url $siteUrl -Interactive
  
$conditionalFormula = "=if[{0}],'true','false')" -f '$myColumnB'
 
# Get SP list field
$field = Get-PnPField -List "Tasks" -Identity "ColumnA"
 
# Apply conditional formula to column
$field.ClientValidationFormula = $conditionalFormula
$field.Update()
Invoke-PnPQuery

 

Replace siteUrl variable value with URL of your SharePoint site and use display name of your list in place of Tasks (in above code).


Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

thank you, I missed that place. I am now able to connect to my spo and make the changes, but now my changes arent working as I believe I have messed up with the logic, tell me , is this okay:

$conditionalFormula = "=if([{0}] = true && [{1}] = true, 'true', 'false')" -f '$myColumnB', '$myColumnC'

BOTH B AND C ARE CHECKBOXES. why isnt this working?

but this doesnt work for me.
my column A or -Identity is " -Identity "MY_X0020_NAME"

@dperez13 Check this article for string formatting in PowerShell: Understanding PowerShell and Basic String Formatting 


Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.